1
votes

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:

Example

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.

1
it would be very helpful if you provide the simple script code to create this neo4j example, it is unclear what nodes have the label REGION, if all the nodes in the middle have REGION label, then all paths should be returned. In a nutshell, you will have to specify (in some way) what paths are desired and which are not. What is the rule you wish to implement? I believe it is still "to be defined" I don't see it here.plastic
@plastic I added a snippet I used to create the graph.Yoni S

1 Answers

1
votes

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