Cypher query - check connection, if not, check another
I would like to check if a relationship exists from a node, and in case it is not found, then I want to check a different type of relationship with the same node.
Something like (a: Type) - [: relation1] - (b) if relation1 exists the query returns node b . If does not exist , then another relation of type (a: Type) - [: relation2] - (b) will be checked and returns node b .
I want to know how this can be written as a single cypher request. Any help would be greatly appreciated. Thank.
+3
nithin ks
source
to share
2 answers
You can use COALESCE () to make a fallback selection if node in the first relation is null.
// after you've already matched to a
OPTIONAL MATCH (a)-[:relation1]-(b)
OPTIONAL MATCH (a)-[:relation2]-(c)
WITH a, COALESCE(b, c) as b // will use node c if b is null
...
+2
InverseFalcon
source
to share
How about using UNION?
MATCH (a:Type)-[:relation1]-(b)
RETURN b
UNION
MATCH (a:Type)-[:relation2]-(b)
RETURN b
Hope this helps, Tom
+2
Tom geudens
source
to share