3
votes

Hi there I've been working with neo4j and tree data and so far it's worked out great. However, I can't seem to find much documentation on how the pattern matching generally works so I can verify if a query I'm making will have the predicted result.

I have a tree with nodes 1, 2, 3, 4, where 2 is a child of 1 and 3 + 4 are both children of 2. There are directed edges from 1 to 2, 2 to 3 and 2 to 4. If I execute the following Cypher query, will I always get the most recent common ancestor for the return node x?

START a = node(3), b = node(4)

MATCH x-[ * ]->a, x-[ * ]->b

return x;

This query returns node 2 for x, but how can I assure myself it will never return 1?

1

1 Answers

3
votes

You could do something like:

start a=node(3), b=node(4)
match pa=x-[*]->a, pb=x-[*]->b
return x
order by length(pa) + length(pb)
limit 1

http://console.neo4j.org/r/ntvwuz