# How to spread the path to childs if relation of parents exists

**URL:** <https://community.neo4j.com/t/how-to-spread-the-path-to-childs-if-relation-of-parents-exists/67891>\
**Category:** Cypher\
**Tags:** cypher\
**Created:** [May 13, 2024, 4:02am UTC](https://community.neo4j.com/t/how-to-spread-the-path-to-childs-if-relation-of-parents-exists/67891 "2024-05-13T04:02:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ngocld](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ngocld/32/29223_2.png) [@ngocld](https://community.neo4j.com/u/ngocld)\
**Post date:** [May 13, 2024, 4:02am UTC](https://community.neo4j.com/t/how-to-spread-the-path-to-childs-if-relation-of-parents-exists/67891/1 "2024-05-13T04:02:43Z")

</div>

I have a test case like this:

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/2/5/25d043059c4881f570834edc7da48749b213d81c.png)

Given A, How to get `F, E, B, C` and their relations.

First I want get all the child nodes `b` via `(a)-[:SubEvent]->(b)` then spread the path by childs of childs with `[:Causal|Result|SubEvent]` of child. if exists child of child then spread `NextEvent` of child of child

I have a cypher like this:

```auto
match (e:A)-[:SubEvent|mo_ta]->(e1)-[r1:NextEvent*0..1]-()
match path=(e1)-[r2:(Causal|Result|SubEvent)*0..1]->(var1)-[r3:NextEvent*0..1]-(var2)
return nodes(path), r1 + r2 + r3

```

but it doesn't work: if `e1` doesn't have `r2` then `e1` equal `var1` then it still gets the NextEvent of `e1` which is `D` which is not correct.

---

<div class="post-metadata">

**Author:** ![ameyasoft](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ameyasoft/32/32639_2.png) [@ameyasoft](https://community.neo4j.com/u/ameyasoft)\
**Post date:** [May 13, 2024, 3:04pm UTC](https://community.neo4j.com/t/how-to-spread-the-path-to-childs-if-relation-of-parents-exists/67891/2 "2024-05-13T15:04:33Z")

</div>

> [@ngocld](#):
>
> Given A, How to get `F, E, B, C` and their relations.
> 
> First I want get all the child nodes `b` via `(a)-[:SubEvent]->(b)` then spread the path by childs of childs with `[:Causal|Result|SubEvent]` of child. if exists child of child then spread `NextEvent` of child of child
> 
> I have a cypher like this:

```auto
Match p=(a)-[*..3]-(d)
return p 
This will return all the node shown in your image. Changing path length to 2 shows a, b, c, d, e nodes
If you do not want to see node d, then use

Match p=(a)-[*..3]-(d) where not labels(d) in [["d"]]

```

---

<div class="post-metadata">

**Author:** ![v2belleville](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/v2belleville/32/29050_2.png) [@v2belleville](https://community.neo4j.com/u/v2belleville)\
**Post date:** [May 31, 2024, 9:31am UTC](https://community.neo4j.com/t/how-to-spread-the-path-to-childs-if-relation-of-parents-exists/67891/3 "2024-05-31T09:31:58Z")

</div>

> [@ngocld](#):
>
> First I want get all the child nodes `b` via `(a)-[:SubEvent]->(b)` then spread the path by childs of childs with `[:Causal|Result|SubEvent]` of child. if exists child of child then spread `NextEvent` of child of child

here is how this translates to cypher

```auto
match (:Event {id:"A"})-[r1:SubEvent]->(subEvent)
optional match (subEvent)-[r2:Causal|Result|SubEvent]->(child:Event)
optional match (child)-[r3:NextEvent]-(e)
return *

```

note that "if exists child of child" is implicit: if the first optional match does not return anything, the second is ignored

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/a/2/a27eb5f37de834784b71491c9e24c40fa97ca97b.png)
