假如有如下节点和边数据:
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