Fetch a tree with Neo4j -
given forest of trees in neo4j rest server, i`m trying return single tree given root vertex.
being each tree quite large, need de-duplicated list of vertices , edges in order able reconstruct full tree on client side.
i tried multiple combinations around match (r:root)-[*]->()
return path starting root, lots of duplicates:
match p = (r:root)-[*]->(x) return nodes(p) vertices, rels(p) edges";
this returns each , every path follows, repeating each node every time:
a->b a->b->c a->b->c->d
etc...
instead, need result having
{ vertices: [a, b, c, d], edges: [[a, b], [b, c], [c, d]] }
i'm using node.js seraph, if relevant, i`m not strictly bound library.
so first off, might want add where
clause make sure path ending leaf:
match p = (r:root)-[*]->(x) not(x-->()) return nodes(p) vertices, rels(p) edges";
so secondly, if want of nodes , relationships in 1 go, may need execute 2 queries:
match p = (r:root)-[*]->(x) not(x-->()) unwind nodes(p) vertex return distinct vertex; match p = (r:root)-[*]->(x) not(x-->()) unwind rels(p) edge return distinct startnode(edge), endnode(edge);
update (michael)
match p = (r:root)-[*]->(x) not(x-->()) unwind nodes(p) vertex collect(distinct vertex) nodes, p unwind rels(p) edge return nodes, collect(distinct edge) rels
update2 (michael)
i found more compact way
match p = (:root)-[r*]->(x) return collect(distinct id(x)) nodes, [r in collect(distinct last(r)) | [id(startnode(r)),id(endnode(r))]] rels
if want include root node, use *0..
Comments
Post a Comment