从 Neo4j 密码查询返回边列表

Returning edge list from Neo4j cypher query

您好,我最近开始使用 Neo,想知道如何查询图表以 return 显示连接的两列 ID。

MATCH path=(s1:Student{StudentId: 7079945})-[r:FRIENDS*6..9]->(s2:Student)
WHERE  s1.StartDate> '2015-01-01' 
RETURN s1.StudentID, s2.StudentId

上面为我提供了 s1 的所有连接,但是它没有显示任何其他连接。如何将此图中的所有连接收集到一个边缘列表中?计划是在 RNeo4j 中使用此查询并使用 igraph 进行分析。

提前致谢

最简单的方法是return路径

MATCH path=(s1:Student{StudentId: 7079945})-[r:FRIENDS*6..9]->(s2:Student)
WHERE  s1.StartDate> '2015-01-01' 
RETURN path

因为您可能对 returning :FRIENDS 关系不感兴趣,因为它们都是相同的,您可以 return 只有路径的节点

MATCH path=(s1:Student{StudentId: 7079945})-[r:FRIENDS*6..9]->(s2:Student)
WHERE  s1.StartDate> '2015-01-01' 
RETURN nodes(path)

您可以在 documentation 中找到更多路径功能。

如果您真的只是对每条路径中的节点对感兴趣,那么您可以执行以下操作。

此查询采用匹配的路径,对每条路径中的节点对进行分组,然后仅 returns 不同的对。

MATCH path=(s1:Student{StudentId: 7079945})-[r:FRIENDS*6..9]->(s2:Student)
WHERE  s1.StartDate> '2015-01-01'
WITH REDUCE(pairs =[],r in relationships(path) | pairs + [[startNode(r).StudentId,endNode(r).StudentId]]) as pairs
UNWIND pairs as pair
WITH DISTINCT pair
RETURN pair[0] as from, pair[1] as to