Iterate over relationships

Hi all,

I have a cypher query like MATCH p=(a)-[r *..X]->(b) ... X is a variable sometimes is 4, sometimes, 6 ,...
Let's say that relationships have a date as a property and I want only paths in which the difference of dates of consecutive relationships is 1 day.
I can do something like this (X=4)
MATCH path = (:Node)-[r1]->()-[r2]->()-[r3]->()-[r4]->(:Node)
WHERE duration.between(r1.date, r2.date).days = 1
AND duration.between(r2.date, r3.date).days = 1
AND duration.between(r3.date, r4.date).days = 1
RETURN path

but this is not univesal.
Is there an option to iterate over relationships and compare dates?

Regards
Andrej

you can iterate over relationships dynamically and compare the dates using a variable-length relationship pattern and the relationships() function. Here's an example of how you can achieve this:

MATCH path = (:Node)-[rels*..X]->(:Node)
WHERE all(idx in range(0, size(rels)-2) WHERE duration.between(rels[idx].date, rels[idx+1].date).days = 1)
RETURN path

1 Like

You can also look at the apoc path procedures. They allow you to specify a max depth/length passed in a config map, so it can be set dynamically.

Path seems sufficient, although there are other procedures in the suite.

1 Like

Thank you for your time and solution

Regards

1 Like