[英]How to query extended path between nodes with Cypher in Neo4J?
I'm using Neo4J / Cypher to store / retrieve some data based on a graph model.我正在使用 Neo4J/Cypher 基于图形模型存储/检索一些数据。
Let's suppose the following model: I have a set of node (type=child) that are connected through a relation (type=CONNECTED_TO).让我们假设以下模型:我有一组通过关系 (type=CONNECTED_TO) 连接的节点 (type=child)。
C1 -[:CONNECTED_TO]-> C2 -[:CONNECTED_TO]-> C3 -[:CONNECTED_TO]-> C4
If I want to query a path starting from C1 to C4 without knowing intermediates:如果我想在不知道中间体的情况下查询从 C1 到 C4 的路径:
MATCH p=
(a:child {id:'c1Id'}) -[:CONNECTED_TO*0..]-(z:child {id:'c4Id'})
RETURN p
So far so good.到现在为止还挺好。
Now suppose that each child is contained in a parent and I want to start the query from parent ID现在假设每个孩子都包含在一个父级中,我想从父级 ID 开始查询
P1 -[:CONTAINS]-> C1
P2 -[:CONTAINS]-> C2
P3 -[:CONTAINS]-> C3
P4 -[:CONTAINS]-> C4
The query looks like:查询如下所示:
MATCH p=
(a:parent {id:'p1Id'})
-[:CONTAINS]->
(cStart:child)
-[:CONNECTED_TO*0..]-
(cEnd:child)
<-[Contains]-
(z:parent {id:'p4Id'})
RETURN p
This give me the good result.这给了我很好的结果。 The following path:
以下路径:
P1 -[:CONTAINS]-> C1 -[:CONNECTED_TO]-> C2 -[:CONNECTED_TO]-> C3 -[:CONNECTED_TO]-> C4 <-[:CONTAINS]- P4
What I would like to do is to query this path from P1 to P4 using the child topology but I want to retrieve also all parents containing intermediates.我想要做的是使用子拓扑查询从 P1 到 P4 的这条路径,但我还想检索包含中间体的所有父级。
How can I improve my last cypher query to return in addition of that:除此之外,我如何改进我的最后一个密码查询以返回:
P2 -[:CONTAINS]-> C2
P3 -[:CONTAINS]-> C3
Is it possible?是否可以? Maybe my model design is not appropriate for that Use case?
也许我的模型设计不适合该用例? In this case, how to improve it to address this query?
在这种情况下,如何改进它以解决此查询?
Tx发送
You can use list comprehension
construct:您可以使用
list comprehension
结构:
MATCH p=
(a:parent {id:'p1Id'})
-[:CONTAINS]->
(cStart:child)
-[:CONNECTED_TO*0..]-
(cEnd:child)
<-[Contains]-
(z:parent {id:'p4Id'})
RETURN p,
[n IN nodes(p)[1..-1] | (n)<-[:CONTAINS]-(:parent)][0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.