cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Cypher for conditional path segments

Hello,

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

1 ACCEPTED SOLUTION

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

View solution in original post

1 REPLY 1

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