1
votes

I'm working with a Neo4j data set that looks like this:

Sample data

I'm trying to write a query that returns all paths from the green node 84 to node 88 that also returns/includes the blue nodes that have a Files relationship to the gray nodes along the paths

So far, my query looks like this:

MATCH (startingstatus:green {id: 84}),
(endingstatus:green {id: 88}),
path = (startingstatus) - [StatusOut*0..5] -> (endingstatus),
(scenario:gray) - [Files] -> (form:blue) 
RETURN *;

The problem is that right now, the query returns every gray - [Files] -> blue relationship in the database (e.g. the image above), I only want the blue nodes with the relationship to the gray node with the ids 4, 20, and 21 since those are the nodes along the path.

I hope this makes sense. I apologize for having to rename some of the nodes.

1

1 Answers

0
votes

Its hard to test the query without a sample data set, but you can try this solution using filter(), nodes() and UNWIND:

MATCH path = (startingstatus:green {id: 84}) - [:StatusOut|:StatusIn*0..5] -> (endingstatus:green {id: 88})
WITH path, filter(node in nodes(path) where node:gray) as grayNodes
UNWIND grayNodes as grayNode
MATCH (grayNode)-[:Files]->(blueNode:blue)
RETURN path, grayNode, blueNode

To explain: I'm extracting the gray nodes from the path. Then I match the blue nodes related to these gray nodes.