normonics
normonics

Reputation: 191

How do I structure a cypher query to include paths between nodes from a MATCH clause without removing nodes that don't match the "full" pattern?

I've tried this a number of ways so I won't list them all. Let me explain what I'm trying to achieve:

Let's say I have 3 types of nodes: A, B, and C, with possible directed links of form (:A)-->(:B) and (:B)-->(:C) and (:C)-->(:C).

What I want are the B and C nodes that come one and two links from a specific A node and any paths that exist that connect two C nodes together via a path that converges on a common C node.

First, I want to return the B and C nodes that come from a specific instance of A like:

MATCH (a:A)-->(b:B)-->(c:C)
RETURN b,c

What I also want to include are all the paths for which the set of nodes c converge on common c nodes. Something like

MATCH path=(c)-[*]->(c2:C)<-[*]-(c)
RETURN path

I've tried a bunch of ways and I usually either lose the (b) and (c) nodes that don't have such a path or else I get too too many (c2) nodes back that are not part of a path that connects a (c) node to another (c) node (such as if I use relationships of the form [*0..] in the query).

Upvotes: 0

Views: 50

Answers (2)

normonics
normonics

Reputation: 191

TheCrusher got me very close and I did a couple of modifications to get what I needed:

MATCH (a:A {name:"a_name"})-->(b:B)-->(c:C)
WITH COLLECT(DISTINCT b) as bs, COLLECT(DISTINCT c) as cs
MATCH convergingPath=(c:C)-[*]->(c2:C)<-[*]-(c1:C)
WHERE c in cs AND c1 in cs AND c<>c1
RETURN bs, cs, convergingPath

Upvotes: 0

TheTeacher
TheTeacher

Reputation: 510

MATCH (a:A)-->(b:B)-->(c:C)
WITH a,COLLECT(DISTINCT b) as bNodes, COLLECT(DISTINCT c) as cNodes 
OPTIONAL MATCH (c:C)-->(c2:C)<--(c1:C)
WHERE c IN cNodes ANd c1 IN cNodes AND c<>c1 
WITH a ,bNodes,cNodes,c2 as convergePoint,COLLECT(DISTINCT c) AS convergingCNodes
RETURN a, bNodes,cNodes,convergePoint ,convergingCNodes 

as far as i understood question, i guess this will return what you are looking for . "a" A node , all the connected B type nodes in bNodes,all the C type nodes in cNodes, if any c Type nodes are converging then converging point C type node is convergePoint and all the converging nodes on that convergePoint node in convergingCNodes

Upvotes: 1

Related Questions