繁体   English   中英

如何在neo4j中从起始节点获取所有可到达的节点(以及它们之间的所有关系?)

[英]how to get all reachable nodes from a starting node (and optionally all relationships between them?) in neo4j

我有5个节点,1、2、3、4、5
5个节点之间的关系如下

1-->2
2-->3
1-->3
2-->4
3-->5
4-->5

在线模型http://console.neo4j.org/r/8h0c91
使用以下密码查询获得一级连接节点

match (n:Person{name:"1"})-[r]-(m:Person) return n,m,r

结果:

n                        r                          m
(20:Person {name:"1"})  []  (20:Person {name:"1"})
(20:Person {name:"1"})  [(20)-[21:Follow]->(22)]    (22:Person {name:"3"})
(20:Person {name:"1"})  [(20)-[20:Follow]->(21)]    (21:Person {name:"2"})

它只能得到1-> 2和1-> 3之间的关系,不能得到2-> 3。
使用以下密码查询y获取第二级连接节点。

match (n:Person{name:"1"})-[r:Follow*0..2]-(m:Person) return n,m,r

结果:

n   m   r
(0:Person {name:"1"})   (0:Person {name:"1"})   []
(0:Person {name:"1"})   (1:Person {name:"2"})   [(0)-[0:Follow]->(1)]
(0:Person {name:"1"})   (2:Person {name:"3"})   [(0)-[0:Follow]->(1), (1)-[2:Follow]->(2)]
(0:Person {name:"1"})   (3:Person {name:"4"})   [(0)-[0:Follow]->(1), (1)-[3:Follow]->(3)]
(0:Person {name:"1"})   (2:Person {name:"3"})   [(0)-[1:Follow]->(2)]
(0:Person {name:"1"})   (1:Person {name:"2"})   [(0)-[1:Follow]->(2), (1)-[2:Follow]->(2)]
(0:Person {name:"1"})   (4:Person {name:"5"})   [(0)-[1:Follow]->(2), (2)-[4:Follow]->(4)]

我无法获得4-> 5和2-> 3的关系。
我的问题是如何获取所有节点以及所有neo4j节点之间的关系。

您可以只排除name匹配项并添加关系的方向,它将返回所有三个关系。

MATCH (n:Person)-[r]->(m:Person) 
RETURN n,m,r

根据您的评论,您正在寻找“起始节点中的所有可达节点(以及它们之间的所有关系)”

在这种情况下, APOC过程中路径扩展器过程应具有所需的内容:

MATCH (n:Person{name:"1"})
CALL apoc.path.subgraphAll(n, {maxLevel:2}) YIELD nodes, relationships
RETURN nodes, relationships

在上方进行编辑以在遍历上添加上限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM