Reputation: 10962
I have a cypher query like this:
MATCH (start:StartLabel)
OPTIONAL MATCH (start)-[:A]->(end1:EndLabel)
OPTIONAL MATCH (start)-[:B|C]->(middle:MiddleLabel)-[:D]->(end2:EndLabel)
WHERE NOT(end1 IS NULL AND end2 IS NULL)
RETURN *
In this graph model there are multiple valid ways to reach (end)
from (start)
but I want to make sure that the result matches at least one of the possible OPTIONAL MATCH
paths. For some reason I continue to get results where both end1
and end2
are NULL
with this WHERE
clause. Am I missing something about how Cypher processes OPTIONAL MATCH
and WHERE
together?
Upvotes: 1
Views: 324
Reputation: 67044
Here is an alternate way to get the results:
MATCH (start:StartLabel)-[:A]->(end:EndLabel)
RETURN start, NULL as middle, end
UNION
MATCH (start:StartLabel)-[:B|C]->(middle:MiddleLabel)-[:D]->(end:EndLabel)
RETURN start, middle, end
Upvotes: 2
Reputation: 10856
I've been told that WHERES
always apply to the most previous (OPTIONAL )MATCH
, so perhaps this will work?
MATCH (start:StartLabel)
OPTIONAL MATCH
(start)-[:A]->(end1:EndLabel),
(start)-[:B|C]->(middle:MiddleLabel)-[:D]->(end2:EndLabel)
WHERE NOT(end1 IS NULL AND end2 IS NULL)
RETURN *
As as aside, I feel like end1 IS NOT NULL OR end2 IS NOT NULL
reads better to me
Upvotes: 0