[英]how to match right and left nodes with cypher and neo4j
I try to use a Neo4J graph database in my project, and I'll try to explain you my problem. 我尝试在项目中使用Neo4J图形数据库,并尝试向您解释我的问题。
I would like to have the longest path, within the limit of 8 nodes, on right and left of each result. 我想在每个结果的右边和左边有最长的路径,在8个节点的范围内。 But I don't know the last node of each end of my graph
但是我不知道图表两端的最后一个节点
The following diagram is a basic example. 下图是一个基本示例。 My graph is built like a chain, like this :
我的图像一条链一样构建:
My DB - Neo4j diagram 我的数据库-Neo4j图
My problem is to find the left and right nodes. 我的问题是找到左右节点。 With this dummy query, I have duplicate results
通过这个虚拟查询,我得到了重复的结果
MATCH p=((nl)<-[:PREV*0..8]-(i)-[:NEXT*0..8]->(nr)) RETURN nodes(p);
This returns too much duplicate results. 这将返回过多的重复结果。 Here some samples of results :
以下是一些结果样本:
i
h | i
g | h | i
...
i | j
i | j | k
...
h | i | j
h | i | j | k
...
a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q
The last result is the only one that interests me. 最后的结果是唯一让我感兴趣的结果。
It seems that Neo4j returns all possible combinations of nodes to the left and to the right within the limit of 8. 看来Neo4j在限制8之内向左和向右返回节点的所有可能组合。
Additional information: 附加信息:
Is it possible to perform this with Cypher? 是否可以使用Cypher执行此操作?
If you only need one, then order by the length of the path and limit to a single result: 如果只需要一个,则按路径长度排序并限制为单个结果:
MATCH p=((nl)<-[:PREV*0..8]-(i)-[:NEXT*0..8]->(nr))
WITH p
ORDER BY length(p) DESC
LIMIT 1
RETURN nodes(p);
Alternately you can match and gather on each side: 或者,您可以在每一侧进行匹配和聚集:
MATCH p=(nl)<-[:PREV*0..8]-(i)
WITH i, nl
ORDER BY length(p) DESC
WITH i, collect(nl) as nodes
MATCH p = (i)-[:NEXT*1..8]->(nr)
WITH nodes, i, nr
ORDER BY length(p) ASC
WITH i, nodes + collect(nr) as nodes
RETURN nodes;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.