Cypher for conditional path segments


I have a Cypher query as follows : MATCH p = (n {t='toto'})-->(n1)-[y]->(n2)-[z]->(n3) RETURN p;

This query works fine for "complete" paths but in my case I am no sure if relations x,y,z exists.

I would like to ask, if there is a way to have OPTIONAL segments inside of the path so I won't get a null if there is a segment missing but all the path until the missing segment.

I guess solution can be OPTIONAL MATCH p0 = (n {t='toto'})-->(n1) OPTIONAL MATCH p1=(n1)-[y]->(n2) OPTIONAL MATCH p2=(n2)-[z]->(n3) RETURN p0,p1,p2 but I want to know if there is a better way of not splitting the path in multiple blocks.

Thank you

you could use quantifiers:
MATCH p = (n {t='toto'})-->(n1)-[y*0..1]->(n2)-[z*0..1]->(n3) RETURN p;

1 Like