Testing when there is no path between nodes

I have a very simple statement and as part of the requirements, I only need to know if there's a path or not between 2 nodes (they are either P or A type, so there's never a 'cross path'.

MATCH p=(A:P|A)-[:partOf*0..]->(B:P|A) 
  WHERE elementID(A)='idA'
  AND elementID(B) = 'idB' 
RETURN p is not null

I get either 'true' or nothing (how do I get false?)

This will only return a result when 'p' exists; otherwise, the query terminates. You need to use the EXISTS predicate.

Try this:

MATCH (n:P|A) WHERE elementID(n) = "idA"
MATCH (m:P|A) WHERE elementID(m) = "idB" 
RETURN EXISTS ( (n)-[:partOf*0..]->(m) ) as pathExists

BTW- you don't need the label constraints since you are explicitly identifying the nodes you want with elementIds.

the only reason I have the labels is for "security" (there are other "partOf" relationships within P - so need to restrict that nobody can do ID's that are not P (A doesn't have that problem).

Cheers as usual Gary !

1 Like