2
votes

I have a Neo graph that if I choose certain relationships, is a tree.

I would like to start at an arbitrary node and find all nodes up to the root. I want returned to me all the nodes in between the two, including the ends. I need the nodes in order, in the case of the example, "a, t, c, d, e, ROOT".

Imagine the tree has a branch that looks like

a-[]->t-[]->c-[]->d-[]->e-[]->ROOT

If the relationships I care about are x, y, and z, and if the root node is called ROOT, and the starting node has a certain id (unique value), I get something like this:

(a {id:an_id})-[*:x|y|z]->(root:ROOT) return a, root

That returns me the ends, but how do I also get the nodes between them?

edit

nodes(...) http://neo4j.com/docs/stable/query-functions-collection.html#functions-nodes

1
btw. it's Cypher with an y :) - Michael Hunger

1 Answers

4
votes

You got it almost right:

You have to assign your expression to a path, .e.g. p.

MATCH p= (a:Leaf {id:an_id})-[:x|:y|:z*]->(root:ROOT)
RETURN p,a,root, nodes(path);

it might be more efficient to use shortestPath

MATCH p= shortestPath((a:Leaf {id:an_id})-[:x|:y|:z*]->(root:ROOT))
RETURN p,a,root, nodes(p);