How to query the node and its any downstream node's property

There is only one type of node which name "User" (in green) and one type of edge which name "sell_product_to" (in green) in my database, i.e., the following

There're five group (or user-chain) in above figure where the name that behind each node is the node's property (username).

Question : I want to query the node which the name is Lin in any Peter's downstream node and return the entire group (user-chain), i.e., the following result

I tried the following cypher:

Match(n:User)-[r:sell_product_to]->(p:User) where n.username ='Peter' AND p.username='Lin' RETURN n,r,p

The result is the following :

image

Obviously it fail since not only the Henry node do not return but also lack other two user-chains where Peter's next user is not Lin but in Peter's downstream.

So, how can I make it? or at least return the node and edge between Peter and Lin but the entire chain don't show, i.e., the following

Thanks.

Try using the variable length path operator (*) to find arbitrary length paths connect Peter and Lin. Something like:

MATCH userPath=(n:User)-[r:sell_product_to*]->(p:User) 
where n.username ='Peter' AND p.username='Lin'
RETURN userPath

Combining @lyonwj's suggestion to get graphs that contain paths between Peter and Lin, then using an APOC path method to get the subgraph originating from the each 'Peter' node.

Match (n:User{username:'Peter'})-[:sell_product_to*]->(:User{username:'Lin'})
with n
CALL apoc.path.subgraphAll(n, {})
YIELD nodes, relationships
RETURN nodes, relationships
1 Like