# How to search for multiple variable length path relationships?

**URL:** <https://community.neo4j.com/t/how-to-search-for-multiple-variable-length-path-relationships/14856>\
**Category:** Cypher\
**Tags:** cypher, operations\
**Created:** [February 18, 2020, 8:00pm UTC](https://community.neo4j.com/t/how-to-search-for-multiple-variable-length-path-relationships/14856 "2020-02-18T20:00:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew.cash](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/matthew.cash/32/3913_2.png) [@matthew.cash](https://community.neo4j.com/u/matthew.cash)\
**Post date:** [February 18, 2020, 8:00pm UTC](https://community.neo4j.com/t/how-to-search-for-multiple-variable-length-path-relationships/14856/1 "2020-02-18T20:00:05Z")

</div>

Please format code + Cypher statements with the code `</>` icon, it's much easier to read.

I am trying to see how to run a MATCH query where I can see all paths from a certain Node with a specific property key to all Nodes with another specific property type but with out going past the first instance of the second node.

Example:

```auto
MATCH p=(c:Customer)-[*..5]-(e:Equip)
WHERE c.CID STARTS WITH "CID12345" AND e.EquipCat CONTAINS "ROUTER"
RETURN p

```

 ![Annotation 2020-02-18 140741](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/7/71908a1bdfe2da4cda8d8e25b73c3376582781b7.png)

I want to do this search for the CID property of the Customer nodes and get the 2 paths to the first Equip node down each path that has the property Router. This path could be 2 relationships away or it could be 8. But, if both paths are only 2 away then [\*..8] will go past the first Equip:Router node and find many others.. is there a relationship rule or some other method I can use to make sure I ONLY get to each of the FIRST Nodes with the property Router?

---

<div class="post-metadata">

**Author:** ![terryfranklin82](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/terryfranklin82/32/5233_2.png) [@terryfranklin82](https://community.neo4j.com/u/terryfranklin82)\
**Post date:** [February 19, 2020, 2:50am UTC](https://community.neo4j.com/t/how-to-search-for-multiple-variable-length-path-relationships/14856/2 "2020-02-19T02:50:57Z")

</div>

> [@matthew.cash](#):
>
> I want to do this search for the CID property of the Customer nodes and get the 2 paths to the first Equip node down each path that has the property Router

Do you mean "the _label_ Router"?

If so, the [apoc subgraph](http://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/path-finding/path-expander/) functions can probably help you out (especially by using a terminatorNode label)

---

<div class="post-metadata">

**Author:** ![matthew.cash](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/matthew.cash/32/3913_2.png) [@matthew.cash](https://community.neo4j.com/u/matthew.cash)\
**Post date:** [February 19, 2020, 4:41pm UTC](https://community.neo4j.com/t/how-to-search-for-multiple-variable-length-path-relationships/14856/3 "2020-02-19T16:41:28Z")

</div>

That's not a bad idea. I have now spent the past few hours trying to figure out how to properly format that code. I have set all Equip nodes with a property key of ROUTER to also have a label of ROUTER as well... how would I format the call?

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [February 25, 2020, 9:56pm UTC](https://community.neo4j.com/t/how-to-search-for-multiple-variable-length-path-relationships/14856/4 "2020-02-25T21:56:31Z")

</div>

Hello,

Were you able to get this working for you?

If not, an example that may work for you may look something like this:

```auto
MATCH (c:Customer)
WHERE c.CID STARTS WITH "CID12345" 
CALL apoc.path.spanningTree(c, {labelFilter:'/ROUTER', maxLevel:5}) YIELD path
RETURN path

```

(it's called "spanningTree" because the set of all paths from it collectively form one...but basically it does the same thing that `subgraphNodes()` does, but returns paths instead of just the nodes at the end of each path)

---

<div class="post-metadata">

**Author:** ![matthew.cash](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/matthew.cash/32/3913_2.png) [@matthew.cash](https://community.neo4j.com/u/matthew.cash)\
**Post date:** [March 6, 2020, 4:26pm UTC](https://community.neo4j.com/t/how-to-search-for-multiple-variable-length-path-relationships/14856/5 "2020-03-06T16:26:12Z")

</div>

> [@andrew\_bowman](#):
>
> MATCH (c:Customer) WHERE c.CID STARTS WITH "CID12345" CALL apoc.path.spanningTree(c, {labelFilter:'/ROUTER', maxLevel:5}) YIELD path RETURN path

This sort of works as do the previous responses others provided me, though I add '-Customer' to prevent it from flowing through another customer to find the routers. The other issue I am coming across though is when one of the pieces of equipment along the path is connected to more than one Router nodes via handoff nodes, then the results of the call may send me to one of those Routers instead of the Router that is actually in the path for the Customers service path. I have not been able to find a way to make sure that the path for that specific CID/Customer is always guaranteed via the call.
