3
votes

In this diagram I would like to find all of the grey nodes circle in red bubble and not any of the other grey nodes. That is to say all nodes with label :OTHER which are sub nodes of the nodes on the path between A and C. NB The Path between A and C could be longer than the 3 nodes shown here.

enter image description here

So what I want to do is get a path p=A...C and subquery every node in the path for -[:HAS]->(n:OTHER) relationship.

However I'm stuck on the sub query as it is not acting on result set of the initial query but rather on all nodes.

So this gives me all grey nodes :( and not just the ones in the red bubble. Please help

match p=(n:MAIN)-[:EXTENDS*]->(m:MAIN) 
where n.name = 'A' AND m.name='C' 
WITH nodes(p) AS collection 
match (l:MAIN)-[:HAS]->(u:OTHER) return u;  //This last part is my subquery
1

1 Answers

10
votes

The problem is nothing in the "MATCH" of your subquery uses the nodes from the result of the first query.

You can explicitly specify the nodes on the desired path A--B with an identifier (e.g. "middle") and then use it in the matching pattern of your subquery,

MATCH p=(n:MAIN)-[:EXTENDS*0..]->(middle:MAIN)-[:EXTENDS*0..]->(m:MAIN)  
WHERE n.name = 'A' AND m.name='C' 
WITH middle
MATCH (middle:MAIN)-[:HAS]->(u:OTHER)
RETURN u