Reputation: 21
I'm using cypher and I need to build a query that returns paths that go from node 'START' to node 'END' only through specified nodes of type 'REGION', as in the picture:
For example I want to get the paths that go only through node 'a' and 'b', so I should get back 'Only 1' and 'Only 2', and not 'Only 3' as it can go through 'c'. My problem is to find a simple way to create a path without specifying which nodes to exclude.
The script I used to create the graph:
MERGE (st:START)
MERGE (nd1:END {name: "Only 1"})
MERGE (nd2:END {name: "Only 2"})
MERGE (nd3:END {name: "Only 3"})
MERGE (nd4:END {name: "Other 2"})
MERGE (a:Region {name: "a"})
MERGE (b:Region {name: "b"})
MERGE (c:Region {name: "c"})
MERGE (d:Region {name: "d"})
MERGE (st)<-[:PART_OF]-(a)
MERGE (st)<-[:PART_OF]-(b)
MERGE (st)<-[:PART_OF]-(c)
MERGE (st)<-[:PART_OF]-(d)
MERGE (a)<-[:DEPENDS_ON]-(nd1)
MERGE (a)<-[:DEPENDS_ON]-(nd2)
MERGE (b)<-[:DEPENDS_ON]-(nd2)
MERGE (a)<-[:DEPENDS_ON]-(nd3)
MERGE (b)<-[:DEPENDS_ON]-(nd3)
MERGE (c)<-[:DEPENDS_ON]-(nd3)
MERGE (a)<-[:DEPENDS_ON]-(nd4)
MERGE (c)<-[:DEPENDS_ON]-(nd4)
Thanks.
Upvotes: 1
Views: 106
Reputation: 21
I managed to do it with the following query:
MATCH (n:START)<-[:PART_OF]-(reg:Region)<-[:DEPENDS_ON]-(nd:END)
WHERE reg.name <> "a" AND reg.name <> "b"
WITH COLLECT(nd) as unaffected
MATCH (affected:END)
WHERE NOT affected in unaffected
RETURN affected
Upvotes: 1