Need only common nodes across multiple paths - Neo4j Cypher -
smy cypher query finds common child nodes several starting nodes. each path has it's node id's extracted , returned resulting in hundreds of rows in each path. see p1 , p2 example (showing 3 rows , 2 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) p1, extract(b in nodes(p2)| b.id) 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
what intersect paths in cypher during query don't have after. in php iterate using:
$result = array_intersect($p1,$p2);
this return 9,3 above example because common nodes shared paths. there way in cypher don't have hundreds of rows returned?
thanks!
i believe meet needs.
here picture of data under consideration.
// match 2 different paths common ending match p1=(n1:node {name: 1 })-[:sub*]->(i) , p2=(n2:node {name: 6 })-[:sub*]->(i) // collect both sets of paths every i, collect(p1) p1, collect(p2) p2 // recombine nodes of first path(s) distinct collections of nodes unwind p1 p unwind nodes(p) n i, p2, collect( distinct n ) p1 // recombine nodes of second path(s) distinct collections of unwind p2 p unwind nodes(p) n i, p1, collect( distinct n ) p2 // return common ending node nodes common each path return i, [n in p1 n in p2 | n.name] n
edit: updated solution include third path
// match 2 different paths common ending 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 every i, collect(p1) p1, collect(p2) p2, collect(p3) p3 // recombine nodes of first path(s) distinct collections of nodes unwind p1 p unwind nodes(p) n i, p2, p3, collect( distinct n ) p1 // recombine nodes of second path(s) distinct collections of unwind p2 p unwind nodes(p) n i, p1, p3, collect( distinct n ) p2 // recombine nodes of third path(s) distinct collections of unwind p3 p unwind nodes(p) n i, p1, p2, collect( distinct n ) p3 // return common ending node nodes common each path return i, [n in p1 n in p2 , n in p3 | n.name] n
Comments
Post a Comment