从多行收集不同的节点和关系

时间:2014-11-20 18:15:36

标签: neo4j cypher

我正在编写查询以从多个路径检索节点和关系:

MATCH path=(p:Label)-[*..100]->() 
RETURN [n in nodes(path) | ID(n)] as nodeIds, 
       [n in nodes(path)] as nodes, 
       [r in  relationships(path) | ID(r)] as relationshipIds, 
       [r in  relationships(path) | type(r)] as relationshipTypes, 
       [r in  relationships(path)] as relationships

但是我有多行(对应于每条路径),可能有相同的数据。

我希望有一行包含所有不同的nodeIdsrelationshipIds,...

谢谢!

1 个答案:

答案 0 :(得分:3)

当我运行此查询时,我没有获得重复数据。但我可以看到为什么你会认为有重复的数据。首先,你可以试试这个:

MATCH path=(p:Label)-[*..100]->() 
WITH DISTINCT(path) as path
RETURN [n in nodes(path) | ID(n)] as nodeIds, 
       [n in nodes(path)] as nodes, 
       [r in  relationships(path) | ID(r)] as relationshipIds, 
       [r in  relationships(path) | type(r)] as relationshipTypes, 
       [r in  relationships(path)] as relationships
ORDER BY length(path) LIMIT 1;

这将确保所有路径都是不同的,这意味着您不能重复数据,但我认为应该已经是这种情况了。按路径长度排序意味着最长路径首先出现,而极限1仅表示最长路径。

无论如何,您可能看到的重复是由路径和路径碎片引起的。假设我有a->b->c。您的查询将报告三个路径:

  • a->b->c
  • a->b
  • b->c

请注意,这是正确的答案。但就节点ID和关系ID而言,您会在结果集中看到大量重复,因为每个节点ID在结果中至少会出现两次。