drdot
drdot

Reputation: 3347

returning a subgraph related to the path

                     (y1:Y) 
                     ^
                     |
(a1:A) -> (b1:B) -> (c1:C) 
                       ^
                       |
                    (s1:S)
                       ^
                       |
                    (z1:Z)    



                    (e1:E)
                     ^
                     |
                    (d1:D)
                     ^
                     |
(a2:A) -> (b2:B) -> (c2:C) -> (y2:Y) 
                       ^
                       |
                    (s2:S)
                       ^
                       |
                    (z2:Z)    
                     



(a3:A) -> (b3:B) -> (c3:C) 
                       ^
                       |
                    (s3:S)
                       ^
                       |   \
                    (z3:Z)  (z4:Z)

I would like to find path between node label A and C and get node label Y and node label D, E if these decorating nodes exists. I also always need to extract the Z nodes that are decorating the S node which decorates the C nodes. There are more than one Z type nodes that can decorate the S node, so I extracted the Z nodes as a list.

So I can use this query

MATCH p=((:A)-[*]->(c:C)), (c)<--(:S)<--(z:Z)
WITH p, z, 
     HEAD([(c)--(y:Y) | y ])  AS yDecoration,
     [(c)-[*]->(d:D)-[*]->(e:E) | [d,e]] AS deDecoration
RETURN p, yDecoration, deDecoration, collect(z) as zDecoration

But what if I want to just return subgraphs instead of splitting the node y, d, e separately in the return? i,e., my return will just be three subgraphs in the above example. Each subgraph contains the path A->C and node, edge relationship between the path nodes, Y and D/E so there is no need to reconstruct the graph if needed later.

// need a cypher query statement 
RETURN subgraph, zDecoration

Upvotes: 1

Views: 49

Answers (2)

Charchit Kapoor
Charchit Kapoor

Reputation: 9284

Probably not the best way to do it, but would this help:

MATCH subgraph=((:A)-[*]->(c:C)-[*]->(:Y)) 
OPTIONAL MATCH (c)<-[*]-(:S)<-[:R]-(z:Z)
WITH subgraph, collect(z) AS zdecoration
RETURN subgraph, zdecoration
UNION ALL
MATCH subgraph=((:A)-[*]->(c:C)-[*]->(:D)-[*]->(:E))
OPTIONAL MATCH (c)<-[*]-(:S)<-[:*]-(z:Z)
WITH subgraph, collect(z) AS zdecoration
RETURN subgraph, zdecoration
UNION ALL
MATCH subgraph=((:A)-[*]->(c:C))
OPTIONAL MATCH (c)<-[*]-(:S)<-[:R]-(z:Z)
WITH subgraph, collect(z) AS zdecoration
RETURN subgraph, zdecoration

Upvotes: 0

jose_bacoy
jose_bacoy

Reputation: 12684

You can use variable path [*0..] to get the nodes connected to C. Zero means C can have no connected node (like C3).

MATCH subgraph=((:A)-[*]->(c:C)-[*0..]->()), (c)<--(:S)<--(z:Z)
RETURN subgraph, collect(z) as zDecoration

Result:

enter image description here

Upvotes: 0

Related Questions