How to match a path using neo4j cypher and in case part of it doesn't exist return the rest

Viewed 1407

I have a Neo4j graph that looks something like this : (person:Person)-[:acted_in]->(movie:Movie)-[:played_in]->(country:Country)

I would like to match the whole path using this query:

MATCH path = ((person:Person)-[:acted_in]->(movie:Movie)-[:played_in]->(country:Country)) RETURN NODES(path), RELATIONSHIPS(path)

but, here is the tricky part, I would also like to add a condition that if [:played_in] does not exist, just return the partial path: (person:Person)-[:acted_in]->(movie:Movie)

Thanks.

1 Answers

Maybe something like the following query will be useful for you :

MATCH path = (a)-[:b]->(c) 
OPTIONAL MATCH path2 =(c)-[:d]->(e) 
RETURN 
   NODES(path)+COALESCE (NODES(path2),[]), 
   RELATIONSHIPS(path)+COALESCE (RELATIONSHIPS(path2),[])
Related