neo4j cypher 如何合并多个路径返回呢?

假如有如下节点和边数据:

match (n1{ID:'8a819bd678c9fc070178ca08ad7046be'})
optional match p1=(n1)-[]->(n2)
optional match p2=(n2)-[]->(n3)
return p1,p2

数据关系如下图:

那么,如何获取所有不重复的边呢?

match (n1{ID:'8a819bd678c9fc070178ca08ad7046be'})    //记录数:1
optional match p1=(n1)-[]->(n2)                        //记录数:4
optional match p2=(n2)-[]->(n3)                        //记录数:13
unwind relationships(p1) as r1                        //记录数:13
unwind relationships(p2) as r2                        //记录数:13
return r1, r2

在上面的数据关系中, 边有 13 + 4 = 17条
如何返回 17 条数据记录呢?

10-25补充:最终通过二开一个函数来解决:

@UserFunction(value = "yindangu.relationships")
@Description("提取多个path中的边:unwind yindangu.relationships([p1, p2]) as r")
public List<Relationship> relationships(//
    @Name("paths") List<Path> paths//
) {
    List<Relationship> relationships = new ArrayList<Relationship>();
    for (Path p : paths) {
        if (p != null) {
            for (Relationship r : p.relationships()) {
                relationships.add(r);
            }
        }
    }
    return relationships;
}

查询语句写法参考:

match(n0:MENUCATALOG)
WHERE 1 = 1
    AND n0.ID = '8a819bd678c9fc070178ca08ad7046be' 
optional match p0_1 = (n0)-[:MENUCATALOG2MENUCATALOG*0..]->(n1:MENUCATALOG)
WHERE 1 = 1 
optional match p1_2 = (n1)-[:MENUCATALOG2MENUITEM*1]->(n2:MENUITEM)
WHERE 1 = 1 
unwind yindangu.relationships([p0_1, p1_2]) AS r
WITH DISTINCT r
RETURN startNode(r) AS s
    ,r.ORDERNO AS o
    ,endNode(r) AS e
阅读 1.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进