Search code examples
neo4jcypher

extract decorating nodes if it exists but still return path if decorating nodes does not exist


I have the following graph

                     (y1:Y) 
                     ^
                     |
(a1:A) -> (b1:B) -> (c1:C) 


                    (e1:E)
                     ^
                     |
                    (d1:D)
                     ^
                     |
(a2:A) -> (b2:B) -> (c2:C) 


(a3:A) -> (b3:B) -> (c3:C) 

I would like to find path between node label A and C. I can use the query

match p=((:A)-[*]->(:C))
return p

But I also want to get node label Y and node label D, E if these decorating nodes exists. If I try:

match p=((:A)-[*]->(cc:C)), (cc)-->(yy:Y), (cc)-[*]->(dd:D)-[*]->(ee:E)
return p, yy, dd, ee

Then it is only going to return the path if the C node has Y, D, E connects to it.

The output that I need is:

a1->b1->c1, y1, null
a2->b2->c2, null, [[d1, e1]]
a3->b3->c3, null, null

I.e., if decorating node does not exist, then just return null. For the array, it can be null or empty array. Also D and E nodes will be group into an array of arrays since there could be many pairs of D and E.

What is the best way to achieve this?


Solution

  • This should do it, returning an empty array for the deDecoration if there aren't any D-E decorations

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

    with this graph (multiple D-E)

    enter image description here

    this query

    MATCH p=((:A)-[*]->(c:C))
    WITH REDUCE(s='' , node IN nodes(p) | s + CASE WHEN s='' THEN '' ELSE '->' END + node.name) AS p, 
         HEAD([(c)--(y:Y) | y.name ])  AS yDecoration,
         [(c)-[*]->(d:D)-[*]->(e:E) | [d.name,e.name]] AS deDecoration
    
     RETURN p, yDecoration, deDecoration
    

    returns

    ╒════════════╤═════════════╤═════════════════════════╕
    │"p"         │"yDecoration"│"deDecoration"           │
    ╞════════════╪═════════════╪═════════════════════════╡
    │"A2->B2->C2"│null         │[]                       │
    ├────────────┼─────────────┼─────────────────────────┤
    │"A1->B1->C1"│null         │[["D2","E2"],["D1","E1"]]│
    ├────────────┼─────────────┼─────────────────────────┤
    │"A3->B3->C3"│"Y1"         │[]                       │
    └────────────┴─────────────┴─────────────────────────┘