gpt4 book ai didi

neo4j - Cypher 查询返回重复的结果

转载 作者:行者123 更新时间:2023-12-02 00:09:59 25 4
gpt4 key购买 nike

我有以下图形设置:

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 返回唯一结果,但是是否可以更改查询以便通过匹配路径仅返回一个结果,而不应用额外的操作(distinct)?

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

最佳答案

如果将 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"})

Actor “A”作为 Actor 2 的值(value)被包括在内!但仔细想想这是有道理的,因为在查询中没有任何地方告诉 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

关于neo4j - Cypher 查询返回重复的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15885985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com