Cypher查询返回重复的结果

时间:2013-04-08 18:11:24

标签: neo4j cypher

我有以下图表设置:

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

测试设置和查询

1 个答案:

答案 0 :(得分: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的值!但是当你考虑它时这是有道理的,因为你在查询中没有告诉neo4j actoractor2需要是不同的实体。

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

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