I want to query all paths of arbitrary length
(a)-[:Relation*]->(b)
where for each pair (r1, r2)
of consecutive relationships
()-[r1]->()-[r2]->()
a condition, say r2.foo > r1.foo
, is met.
You can imagine foo
being a timestamp and I only want to find paths where all steps are in chronological order.
What I currently have is this:
MATCH path = ()-[:Relationship*]->()
WITH rels(path) AS rels, path
WHERE reduce(acc = { inOrder: true, previous: rels[0] }, r IN tail(rels) |
{ inOrder: acc.inOrder AND r.foo > acc.previous.foo, previous: r }).inOrder
RETURN path
Now, this seems to work. However, I'm not sure if this is the most efficient query. Is there a better way to do it?