在 Neo4j 中查询特定于案例的节点 [英] Query case-specific nodes in neo4j
问题描述
我有一个像附加图片一样的设置.橙色节点表示案例,蓝色节点表示该案例中各种活动的表演者.
I have a setup like the attached image. Orange nodes denotes the cases and the Blue nodes denotes the Performers of various activities within that case.
我想依次查询每个案例.在每种情况下,我都需要添加关系
I would like to query each case in turn. Within each case, I need to add relationship
[:RELATED {value: 1}]
from nodei 到 nodek ,对于位于其间的所有 k(ID 落在在 i 和 j 之间)那对 Performer 节点(nodei,nodej)使得:
名称(节点i) == 名称(节点j)
和
|ID(nodei) - ID(nodej) |>= 2
from nodei to nodek , for all k that lies in between(nodes whose ID fall between i and j) those pair of Performer nodes (nodei,nodej) such that :
Name(nodei) == Name( nodej )
and
| ID(nodei) - ID(nodej) | >= 2
[示例和预期输出]:
在 Case1 中,由于 ID:1 和 ID:4 的 Performer 节点满足条件,因此在以下之间添加关系:
In Case1, since Performer nodes with ID:1 and ID:4 satisfy the criteria, so relationships are added between:
Node(ID:1) to Node(ID:2)
Node(ID:1) to Node(ID:3)
在 Case2 中,ID:2 和 ID:4 的 Performer 节点满足条件,因此添加了以下关系:
In Case2, Performer nodes with ID:2 and ID:4 satisfy the criteria, so relationships are added between:
Node(ID:2) to Node(ID:3)
在 Case3 中,有两组节点满足条件,
In Case3, there are two sets of nodes satisfying the criteria,
一个.为节点(ID:1)和节点(ID:4)添加来自
a. For Node(ID:1) and Node(ID:4) add relationships from
Node(ID:1) to Node(ID:2)
Node(ID:1) to Node(ID:3)
B.对于节点(ID:3)和节点(ID:5),添加来自
b. For Node(ID:3) and Node(ID:5), add relationship from
Node(ID:3) to Node(ID:4)
在为上述情况制定 CYPHER 查询时需要提示.
Hint needed in formulating CYPHER queries for the above case.
提前致谢.
推荐答案
正如问题评论中提到的,你的计算应该颠倒:
As mentionned in the question comment, your calculation should be reversed :
node j - node i >= 2
还有一点,对于情况3,按照你的解释,节点2和3之间应该有第三种关系
One point more, for case 3, Following your explanations there should be a third relationship between node 2 and 3
这是我做的一个查询,你可以在这个 Neo4j 控制台中测试它:http://console.neo4j.org/r/gpfesu
Here is a query I made, you can test it in this neo4j console : http://console.neo4j.org/r/gpfesu
MATCH (n:Case)
MATCH path=(pe)<--(n)-->(pe2)
WHERE pe.name = pe2.name
AND pe2.id - pe.id >= 2
WITH path,n, (pe2.id - pe.id) as length
WITH range(head(nodes(path)).id+1, last(nodes(path)).id-1) as ids,
n,
head(nodes(path)).id as starter,
length
UNWIND ids as x
MATCH (perf:Performer {id:starter})<--(n)-->(perf2:Performer {id:x})
MERGE (perf)-[:RELATES_TO {value:1, length:length}]->(perf2)
这篇关于在 Neo4j 中查询特定于案例的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!