cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Traverse graph recursively

jeet
Node Link

Seed data

when i run below query
MATCH (start:Part)-[]->(bom:BomProperties)-[]->(end:Part) return start{.*},end{.*},bom{.*};

i get desired result.

which explains or read as part 100 is connected to 101,102,103 with qtry 30,60,90 respectively and so on giving me the complete relations and node in the DB.

however now I want to get only the subset of the data for eg. when i run
match (start:Part{partNumber: "103", revision: "A"})-[]->(bom:BomProperties)-[]->(end:Part) return start{.*},end{.*},bom{.*};

it gives only 2 rows.

however it should give me all the nodes connect further from super coach (part 209) as well..
i.e it should run traverse till the end node from all the possible and connected paths however i am unable to get the below result. when i add attribute filter.

My intention is to for the given data i should give the startnode and it should go and get all the connected nodes

+----------------------------------------------------------------------------------------------------------------------------------+
| start | end | bom |
+----------------------------------------------------------------------------------------------------------------------------------+
| {partNumber: "103", partName: "Train", revision: "A"} | {partNumber: "209", partName: "Super Coach", revision: "A"} | {Qty: 120} |
| {partNumber: "103", partName: "Train", revision: "A"} | {partNumber: "105", partName: "Seat", revision: "A"} | {Qty: 100} |
| {partNumber: "209", partName: "Super Coach", revision: "A"} | {partNumber: "104", partName: "Engine", revision: "A"} | {Qty: 23} |
| {partNumber: "104", partName: "Engine", revision: "A"} | {partNumber: "107", partName: "Bolt", revision: "A"} | {Qty: 10} |
| {partNumber: "104", partName: "Engine", revision: "A"} | {partNumber: "106", partName: "Nut", revision: "A"} | {Qty: 20} |
+----------------------------------------------------------------------------------------------------------------------------------+

1 REPLY 1

You want variable-length pattern matching:

This allows you to define a pattern where a relationship can be traversed some number of times to reach some other node.

You can assign a path variable to the result of the MATCH, and get the nodes or relationships like so:

match path = (start:Part {partNumber: "103", revision: "A"})-[*]->(end:Part) 
where all(node in nodes(path) WHERE node:Part OR node:BomProperties)
...

The problem is that your bom nodes sit between the parts, making it a little bit tricky to get the results you want.

One approach is to use the variable-length matching to only get the parts in question, then collect those nodes and UNWIND them and repeat your earlier MATCH to get the results in the same format as before:

MATCH path = (start:Part {partNumber: "103", revision: "A"})-[0*]->(part:Part) 
WHERE all(node in nodes(path) WHERE node:Part OR node:BomProperties)
WITH collect(DISTINCT part) as parts
UNWIND parts as start
MATCH (start:Part)-[]->(bom:BomProperties)-[]->(end:Part)
RETURN  start{.*},end{.*},bom{.*};