3

我有以下图表设置:

start root=node(0)
create (F {name:'FRAME'}), (I {name: 'INTERACTION'}), (A {name: 'A'}), (B {name: 'B'}),
root-[:ROOT]->F, F-[:FRAME_INTERACTION]->I, I-[:INTERACTION_ACTOR]->A, I-[:INTERACTION_ACTOR]->B

以下查询返回重复的结果:

START actor=node:node_auto_index(name='A') 
MATCH actor<-[:INTERACTION_ACTOR]-interaction-[:INTERACTION_ACTOR]->actor2,
   frame-[:FRAME_INTERACTION]->interaction 
RETURN frame, interaction

Query Results

+-----------------------------------------------------+
| frame                 | interaction                 |
+-----------------------------------------------------+
| Node[1]{name:"FRAME"} | Node[2]{name:"INTERACTION"} |
| Node[1]{name:"FRAME"} | Node[2]{name:"INTERACTION"} |
+-----------------------------------------------------+
2 rows
52 ms

即使我再添加一个开始节点来尝试限制结果,我也有相同的结果:

START actor=node:node_auto_index(name='A'), frame=node:node_auto_index(name='FRAME') 
MATCH actor<-[:INTERACTION_ACTOR]-interaction-[:INTERACTION_ACTOR]->actor2,
   frame-[:FRAME_INTERACTION]->interaction 
RETURN frame, interaction

我想了解为什么查询返回重复的结果。我知道可以通过使用 distinct 返回唯一的结果,但是是否可以更改查询以便通过匹配路径仅返回一个结果,而不应用额外的操作(不同的)?

(设置和查询可以在http://console.neo4j.org/?id=q2e0ay进行测试)

4

1 回答 1

3

如果您添加actor2到退货列表中,您将看到问题所在:

 frame                 interaction                 actor             actor2
(7 {name:"FRAME"})    (8 {name:"INTERACTION"})    (9 {name:"A"})    (9 {name:"A"})
(7 {name:"FRAME"})    (8 {name:"INTERACTION"})    (9 {name:"A"})    (10 {name:"B"})

演员“A”作为actor2的值被包括在内!但是当您考虑它时,这是有道理的,因为您在查询中没有任何地方告诉 neo4jactor并且actor2需要是不同的实体。

幸运的是,这很容易做到:

START actor=node:node_auto_index(name='A') 
MATCH actor<-[:INTERACTION_ACTOR]-interaction-[:INTERACTION_ACTOR]->actor2,
    frame-[:FRAME_INTERACTION]->interaction 
WHERE actor <> actor2      //like this!
RETURN frame, interaction
于 2013-04-08T23:11:28.020 回答