首页 文章

返回Cypher路径查询中的唯一节点

提问于
浏览
12

我正在尝试检索链接到给定图节点的一组唯一元素 . 我将一些节点加载到Neo4j图数据库中,这些数据库使用'TO'关系连接(例如节点6连接'TO'节点7) . 我已经能够检索我的起始节点和通过'TO'关系链接的其他路径之间的所有路径,使用:

start a = node(6)
    match p = (a)-[r:TO*..]->(b)
    return distinct EXTRACT(n in nodes(p): n);

这给了我不同的输出路径,但仍然有重复的节点值,例如:

+-------------------------------------------------------+
    | p                                                     |
    +-------------------------------------------------------+
    | [Node[6]{},:TO[5] {},Node[7]{}]                       |
    | [Node[6]{},:TO[5] {},Node[7]{},:TO[9] {},Node[11]{}]  | 
    etc...

How can I combine these paths into a single list containing unique path values? 我尝试过使用COLLECT,但这只会产生上述结果的嵌套版本:

start a = node(6) 
    match p = (a)-[r:TO*..]->(b) 
    return collect(distinct p);

    [[Node[6]{},:TO[5] {},Node[7]{}],[Node[6]{},:TO[5] {},Node[7]{},:TO[9] {}, ... ]

1 回答

  • 21

    我对你想要的结果感到困惑(如果不对,你能给出一个例子吗?) . 你想要路径还是想要节点?如果你想要节点,那么也许你只想要:

    start a = node(6)
    match (a)-[:TO*]->(b)
    return collect(distinct b);
    

相关问题