只需要跨多个路径的公共节点-Neo4j Cypher [英] Need only common nodes across multiple paths - Neo4j Cypher

查看:271
本文介绍了只需要跨多个路径的公共节点-Neo4j Cypher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sMy Cypher查询可找到几个起始节点的公共子节点.每个路径仅提取并返回其节点ID,从而导致每个路径中有数百行.请参见p1和p2示例(仅显示3行和两个起点).

sMy Cypher query finds the common child nodes for several starting nodes. Each path has only it's node id's extracted and returned resulting in hundreds of rows in each path. See p1 and p2 example (showing only 3 rows and two start points).

Match p1=(n1:node{name:"x" })-[r:sub*]->(i)), p2=(n2:node{name:"y" })-[r:sub*]->(i))
RETURN DISTINCT i, extract(a IN nodes(p1)| a.id) as p1, extract(b IN nodes(p2)| b.id) as p2

----RESULTS----

p1=1,4,3
p1=1,8,3
p1=1,8,9,3 

p2=6,7,3
p2=6,5,9,3
p2=6,7,10,3

我想要的是在查询过程中将密码中的路径相交,这样我以后就不必这样做了.在php中,我将使用以下方法进行迭代:

What I would like is to intersect the paths in cypher during the query so that I don't have to do it after. In php I would iterate using:

$result = array_intersect($p1,$p2);

上面的示例将返回9,3,因为它们是所有路径共享的唯一公共节点.在Cypher中有没有办法做到这一点,这样我就不会返回数百行了?

This would return 9,3 from the above example because they are the only common nodes shared by all paths. Is there a way to do this in Cypher so that I don't have hundreds of rows returned?

谢谢!

推荐答案

我相信这会满足您的需求.

I believe this will meet your needs.

这里是所考虑的数据的图片.

Here is a picture of the data under consideration.

// match the two different paths with the common ending i
match p1=(n1:Node {name: 1 })-[:SUB*]->(i)
, p2=(n2:Node {name: 6 })-[:SUB*]->(i)

// collect both sets of paths for every 
with i, collect(p1) as p1, collect(p2) as p2

// recombine the nodes of the first path(s) as distinct collections of nodes
unwind p1 as p
unwind nodes(p) as n
with i, p2, collect( distinct n ) as p1

// recombine the nodes of the second path(s) as distinct collections of     
unwind p2 as p
unwind nodes(p) as n
with i, p1, collect( distinct n ) as p2

// return the common ending node with the nodes common to each path
return i, [n in p1 where n in p2 | n.name] as n

编辑:更新了解决方案以包含第三条路径

updated solution to include a third path

// match the two different paths with the common ending i
match p1=(n1:Node {name: 1 })-[:SUB*]->(i)
, p2=(n2:Node {name: 6 })-[:SUB*]->(i)
, p3=(n3:Node {name: 4 })-[:SUB*]->(i)

// collect both sets of paths for every 
with i, collect(p1) as p1, collect(p2) as p2, collect(p3) as p3

// recombine the nodes of the first path(s) as distinct collections of nodes
unwind p1 as p
unwind nodes(p) as n
with i, p2, p3, collect( distinct n ) as p1

// recombine the nodes of the second path(s) as distinct collections of     
unwind p2 as p
unwind nodes(p) as n
with i, p1, p3, collect( distinct n ) as p2

// recombine the nodes of the third path(s) as distinct collections of     
unwind p3 as p
unwind nodes(p) as n
with i, p1, p2, collect( distinct n ) as p3

// return the common ending node with the nodes common to each path
return i, [n in p1 where n in p2 and n in p3 | n.name] as n

这篇关于只需要跨多个路径的公共节点-Neo4j Cypher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆