user1690293
user1690293

Reputation: 423

making a neo4j/cypher query stop when a condition is true

I have a graph where each node represents a java class and each one has a property called namespace. I want to match a pattern where the starting node and final node share a namespace but all intermediates do not share that namespace. for the case where theres 3 classes (Class A -> Class B -> Class C) i have:

START inside1 = node(*) 
match inside1 -[:USES]-> outside1 -[:USES] -> inside2  
where inside1.namespace <> outside1.namespace 
  and inside2.namespace = inside1.namespace  
return inside1.name, outside1.name, inside2.name

This seems to work fine. When I tried to expand it, I tried:

START inside1 = node(*) 
match inside1 -[:USES]-> outside1 -[:USES*] -> inside2  
where inside1.namespace <> outside1.namespace 
  and outside1.namespace <> inside1.namespace 
  and inside2.namespace = inside1.namespace  
return inside1.name, outside1.name, inside2.name

The problem is I dont want any paths where intermediate nodes share the same namespace as inside1. so my question is, is there anyway that I can tell it "stop when you hit a node whos namespace is equal to inside1.namespace"?

Thanks.

Upvotes: 3

Views: 567

Answers (1)

Eve Freeman
Eve Freeman

Reputation: 33175

Not sure if it's the most succinct, but I think this does what you want? First it gets the general case with your query, then it limits it to only the tightest match with a with.

 START inside1 = node(*) 
 MATCH inside1-[:USES*]->outside1-[:USES]->inside2 
 WHERE inside1.ns <> outside1.ns 
   AND inside2.ns = inside1.ns 
  WITH inside1, inside2, outside1 
 MATCH inside1-[:USES]->outside2 
 WHERE inside1.ns <> outside2.ns 
RETURN inside1, outside1, inside2

http://console.neo4j.org/r/m8iqo5

Upvotes: 3

Related Questions