Cypher查询返回A所涉及的所有关系的集合

时间:2013-06-07 08:01:58

标签: neo4j cypher

给出如下数据库:

    A       F
   / \       \
  B   C       G
 /
D

我想要一组A所涉及的所有关系。这个查询是我能做的最好的,但返回A图中的所有节点对,重复共享关系:

START start = node(A) 
MATCH (start)-[rel*]->(child)
RETURN rel, child;

如何做到最好?

1 个答案:

答案 0 :(得分:3)

我需要这样的东西(由Peter Neubauer建议):

START s = node:node_auto_index(name="A") 
MATCH (s)-[:CHILD*0..]->(parent),        
      path=(parent)-[:CHILD]->(child),       
      (child)-[:CHILD*0..]->(leaf) 
WHERE NOT leaf-[:CHILD]->() 
WITH DISTINCT path AS path 
RETURN EXTRACT(n IN NODES(path): n.name)

草绘on the neo4j website

更简洁的版本:

START s = node:node_auto_index(name="A") 
MATCH (s)-[:CHILD*0..]-( ss ),
      path = ( ss )-[:CHILD]->(child) 
WITH DISTINCT path AS path 
RETURN EXTRACT(n IN NODES(path): n.name)