# Expand nodes around a path of nodes

**URL:** <https://community.neo4j.com/t/expand-nodes-around-a-path-of-nodes/46602>\
**Category:** Cypher\
**Tags:** cypher\
**Created:** [October 28, 2021, 12:43pm UTC](https://community.neo4j.com/t/expand-nodes-around-a-path-of-nodes/46602 "2021-10-28T12:43:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pragyasood28](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/pragyasood28/32/18460_2.png) [@pragyasood28](https://community.neo4j.com/u/pragyasood28)\
**Post date:** [October 28, 2021, 12:43pm UTC](https://community.neo4j.com/t/expand-nodes-around-a-path-of-nodes/46602/1 "2021-10-28T12:43:26Z")

</div>

Hi All,

Suppose I have this path in my graph, and want to return this path.

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/0/8/08d9a1ff2018a6ded17742fc796d3e36a276e8d5.png)

I have written the below query but if I write this I am not getting the Capacity Node. I want to expand the node connected to the Production Node.

```auto

match p=(:WIPCapacity)-[:throughput]->(d:WIPProduction)-[r4:transport]->(e:WIPStorage)-[r5:transport]->(f:WIPStorage)-[r7:transport]->(h:Production)-[r8:transport]->(i:Storage)-[r9:transport]->(j:Storage)-[r10:transport]->(k:Storage)
return p;

```

Please let me know if you can suggest how to write the cypher query to get the full path.

Thanks,  
Pragya

---

<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:** [October 29, 2021, 5:13am UTC](https://community.neo4j.com/t/expand-nodes-around-a-path-of-nodes/46602/2 "2021-10-29T05:13:46Z")

</div>

```auto
Try this:

MATCH (a:WIPCapacity)

//Add this to select a specific node.....
//WHERE a.name = "CAP1"
//End....

CALL apoc.path.spanningTree(a, {}) YIELD path
return path

```

---

<div class="post-metadata">

**Author:** ![pragyasood28](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/pragyasood28/32/18460_2.png) [@pragyasood28](https://community.neo4j.com/u/pragyasood28)\
**Post date:** [October 29, 2021, 3:31pm UTC](https://community.neo4j.com/t/expand-nodes-around-a-path-of-nodes/46602/3 "2021-10-29T15:31:30Z")

</div>

Hi Ameya,

Thanks  
This works and it gives me the full path, but if I know the name of `Capacity Node`, and want to return the Node `WIP Capacity` which is connected to that so for this I tried something similar like you did using apoc.

I tried this, but its not returning the WIPCapacity, its giving Capacity Node only in output:

```auto
MATCH (p:Capacity {name: "CAP1"})
CALL apoc.path.subgraphNodes(p, {
    relationshipFilter: "transport|throughput", labelFilter: "WIPCapacity"
    minLevel: 0,
    maxLevel: 8
})
YIELD node
RETURN node;

```

---

<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:** [November 1, 2021, 6:31am UTC](https://community.neo4j.com/t/expand-nodes-around-a-path-of-nodes/46602/4 "2021-11-01T06:31:16Z")

</div>

> [@pragyasood28](#):
>
> ```auto
> MATCH (p:Capacity {name: "CAP1"})
> CALL apoc.path.subgraphNodes(p, {
> relationshipFilter: "transport|throughput", labelFilter: "WIPCapacity"
> minLevel: 0,
> maxLevel: 8
> })
> YIELD node
> RETURN node;
> 
> ```

```auto
Try this:

MATCH (p:Capacity {name: "CAP1"})
CALL apoc.path.subgraphNodes(p, {
    relationshipFilter: '<transport|throughput',
    minLevel: 0,
    maxLevel: 8
})
YIELD node
RETURN node;
Change the relationshipFilter to this to go towards 'Storage' nodes. '<' indicates 'incoming' diretction
relationshipFilter: 'transport|throughput'

```

---

<div class="post-metadata">

**Author:** ![pragyasood28](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/pragyasood28/32/18460_2.png) [@pragyasood28](https://community.neo4j.com/u/pragyasood28)\
**Post date:** [November 8, 2021, 5:25am UTC](https://community.neo4j.com/t/expand-nodes-around-a-path-of-nodes/46602/5 "2021-11-08T05:25:14Z")

</div>

I tried this, still its returning the Capacity Node in the output and not giving me the WIPCapacity Node which is expected.

---

<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:** [November 8, 2021, 7:35am UTC](https://community.neo4j.com/t/expand-nodes-around-a-path-of-nodes/46602/6 "2021-11-08T07:35:01Z")

</div>

> [@ameyasoft](#):
>
> ```auto
> MATCH (p:Capacity {name: "CAP1"})
> CALL apoc.path.subgraphNodes(p, {
> relationshipFilter: '<transport|throughput',
> minLevel: 0,
> maxLevel: 8
> })
> YIELD node
> RETURN node;
> 
> ```

```auto
Here is my result:
I created this:
merge (a:WIPProduction {name: "WIPP1"})
merge (b:WIPCapacity {name: "CAP1"})

merge (c1:WIPStorage {name: "STOR1"})
merge (c2:WIPStorage {name: "STOR2"})

merge (a1:Production {name: "P1"})

merge (b)-[:throughput]->(a)
merge (a)-[:transport]->(c1)
merge (c1)-[:transport]->(c2)
merge (c2)-[:transport]->(a1)

merge (b1:Capacity {name: "CAP2"})
merge (b1)-[:throughput]->(a1)

merge (c3:Storage {name: "STOR3"})
merge (c4:Storage {name: "STOR4"})
merge (c5:Storage {name: "STOR5"})

merge (a1)-[:transport]->(c3)
merge (c3)-[:transport]->(c4)
merge (c4)-[:transport]->(c5)

```

Result:  
 ![Screen Shot 2021-11-07 at 11.28.22 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/8/9/89bbdd9898855b2f46865b9142c5b0087b691b0c.png)

```auto
Ran this query:
MATCH (p:Capacity {name: "CAP2"})
CALL apoc.path.subgraphNodes(p, {
    relationshipFilter: '<transport|throughput',
    minLevel: 0,
    maxLevel: 8
})
YIELD node
RETURN node;

```

Result:  
 ![Screen Shot 2021-11-07 at 11.32.22 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/1/d/1d3b401971c66222befb0e0793b927b06b9b52ba.png)

---

<div class="post-metadata">

**Author:** ![pragyasood28](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/pragyasood28/32/18460_2.png) [@pragyasood28](https://community.neo4j.com/u/pragyasood28)\
**Post date:** [November 8, 2021, 7:46am UTC](https://community.neo4j.com/t/expand-nodes-around-a-path-of-nodes/46602/7 "2021-11-08T07:46:28Z")

</div>

Thank You for the explanation, I am also getting this correct till here but instead of this full path that we get in result can I just get the CAP1 node and its properties ?  
Is there a way to put where condition, as my main goal is to check the properties of CAP1 and CAP2 which are on the same path, and compare the value of a particular property, then return the node where the value of a particular property( Required\_Capacity) is greater.

I hope I am able to explain my use case properly.

Thanks,  
Pragya

---

<div class="post-metadata">

**Author:** ![pragyasood28](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/pragyasood28/32/18460_2.png) [@pragyasood28](https://community.neo4j.com/u/pragyasood28)\
**Post date:** [November 8, 2021, 8:02am UTC](https://community.neo4j.com/t/expand-nodes-around-a-path-of-nodes/46602/8 "2021-11-08T08:02:49Z")

</div>

```auto
MATCH (p {name: "CAP2"})
CALL apoc.path.subgraphNodes(p, {
    relationshipFilter: "<transport|throughput", 
    minLevel: 0,
    maxLevel: 8
})
YIELD node
where node.Available_Capacity<node.Required_Capacity 
return node.name, node.Required_Capacity-node.Available_Capacity as d order by d desc;

```

I tried this putting where condition and returning the desired propeties and this seems to give me the correct output.

I hope this implementation is correct and we can add further steps if required in this query.
