简体   繁体   English

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

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

I have 5 nodes,1,2,3,4,5 我有5个节点,1、2、3、4、5
the relationship between 5 nodes as below, 5个节点之间的关系如下

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

online model http://console.neo4j.org/r/8h0c91 在线模型http://console.neo4j.org/r/8h0c91
use below cypher query to get one level connection nodes 使用以下密码查询获得一级连接节点

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

result: 结果:

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"})

which only can get the relationship between 1-->2 and 1-->3,cannot get 2-->3. 它只能得到1-> 2和1-> 3之间的关系,不能得到2-> 3。
use below cypher query y to get sencond level connection nodes. 使用以下密码查询y获取第二级连接节点。

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

result: 结果:

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)]

i cannot get the relationship of 4-->5 and 2-->3. 我无法获得4-> 5和2-> 3的关系。
my question is how to get all nodes and relationship between all neo4j nodes. 我的问题是如何获取所有节点以及所有neo4j节点之间的关系。

You could just exclude the name match and add the direction of the relationship and it will return all three. 您可以只排除name匹配项并添加关系的方向,它将返回所有三个关系。

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

From your comments, you're looking for 'all reachable nodes from a starting node (and optionally all relationships between them)' 根据您的评论,您正在寻找“起始节点中的所有可达节点(以及它们之间的所有关系)”

In this case, the path expander procedures in APOC Procedures should have what you want: 在这种情况下, APOC过程中路径扩展器过程应具有所需的内容:

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

Edited above to add an upper bound on the traversals. 在上方进行编辑以在遍历上添加上限。

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

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