# How to return variable length pattern in a quite complex DB

**URL:** <https://community.neo4j.com/t/how-to-return-variable-length-pattern-in-a-quite-complex-db/72739>\
**Category:** Cypher\
**Tags:** browser, cypher\
**Created:** [February 21, 2025, 12:08am UTC](https://community.neo4j.com/t/how-to-return-variable-length-pattern-in-a-quite-complex-db/72739 "2025-02-21T00:08:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paolodipietro58](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/paolodipietro58/32/21045_2.png) [@paolodipietro58](https://community.neo4j.com/u/paolodipietro58)\
**Post date:** [February 21, 2025, 12:08am UTC](https://community.neo4j.com/t/how-to-return-variable-length-pattern-in-a-quite-complex-db/72739/1 "2025-02-21T00:08:38Z")

</div>

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

Please provide the following information if you ran into a more serious issue:

- neo4j version: 5.26.1 enterprise
- desktop version: 1.6.1
- browser version: 5.26.2

If you try to import this fragment

```auto
MERGE (n109:Property:ItemPrice {uuid: "5187a41e-bb42-4ecb-aaf9-94bcd536cec7"})<-[:SELLS]-(`DB:Customer`:Customer {name: "ShoeBrand", uuid: "50b8fc32-ceeb-4b5c-8d9e-c3d91214d924"})-[:HAS_CATEGORY]->(n93:Category {name: "Shoe", uuid: "d35e357c-e72a-4992-a27e-dea13717c0d6"})<-[:PRODUCES]-(n98:Property:Brand {name: "Ash", uuid: "c1967b63-59bc-46c0-9efd-c38b8a275101"})<-[:HAS_PROPERTY]-(n112:Product {name: " Sneakers Ash Race", description: "", uuid: "3b264a59-f0a6-463a-8782-7775d08ee19e"})-[:HAS_PROPERTY]->(n115:Color {uuid: "25f26078-d410-4f18-a11d-68a5e4624cf7", name: "beige", rgb: ""})-[:HAS_PROPERTY]->(:Property:Number {number: 36, availability: 2})
MERGE (n98)-[:PRODUCES]->(n128:Category {name: "Sneakers", uuid: "003f4088-4ded-41dd-ba51-1a8a117c1a34"})-[:HAS_PRODUCT]->(:Product {name: " Sneakers rete bianca", description: "", uuid: "d2bb3356-ba61-4226-b1c8-8d15b70c02c4"})-[:HAS_PROPERTY]->(n98)<-[:HAS_PROPERTY]-(:Product {name: " Sneakers Ash tessuto bianco", description: "", uuid: "6b11d1b3-a9bc-4545-9d32-caad40c2ddf3"})<-[:HAS_PRODUCT]-(n128)-[:HAS_PRODUCT]->(n112)
MERGE (n112)-[:HAS_PROPERTY]->(n113:Property:Color {uuid: "54879b9b-d7a2-46d3-ba22-871278cf0c72", name: "black", rgb: ""})-[:HAS_PROPERTY]->(:Property:Number {number: 36, availability: 5})
MERGE (:Property:Part:Upper {material: "leather", percentage: ""})<-[:HAS_PROPERTY]-(n113)-[:HAS_PROPERTY]->(n109)-[:HAS_PRICE {from: "2025-01-01"}]->(:Price {listPrice: 310.00, currency: "EUR", discount: "50%", discountedPrice: 155.00})
MERGE (:Property:Number {number: 38, availability: 5})<-[:HAS_PROPERTY]-(n113)-[:HAS_PROPERTY]->(:Property:Number {number: 37, availability: 5})
MERGE (:Property:Number {number: 40, availability: 5})<-[:HAS_PROPERTY]-(n113)-[:HAS_PROPERTY]->(:Property:Number {number: 39, availability: 4})
MERGE (:Property:Number {number: 40, availability: 6})<-[:HAS_PROPERTY]-(n115)-[:HAS_PROPERTY]->(:Property:Number {number: 38, availability: 3})
MERGE (`DB:Customer`)-[:SELLS]->(n126:Property:ItemPrice {uuid: "2c815d01-e064-4989-a9a4-1a8c17d91c2d"})<-[:HAS_PROPERTY]-(n115)-[:HAS_PROPERTY]->(:Property:Number {number: 37, availability: 0})
MERGE (n126)-[:HAS_PRICE {from: "2025-01-01"}]->(:Price {listPrice: 310.00, currency: "EUR", discount: "50%", discountedPrice: 155.00})
MERGE (n93)-[:HAS_CATEGORY]->(n128)<-[:PRODUCES]-(:Property:Brand {name: "Be Positive", uuid: "cf1edf08-7436-400b-9391-0c8509607a42"})
MERGE (:Property:Brand {name: "Casadei", uuid: "034d6fff-b0b7-4b04-b28b-ac44ab7734d1"})-[:PRODUCES]->(n128)<-[:PRODUCES]-(:Property:Brand {name: "Dansko", uuid: "4606244c-a548-451c-8b4a-273ac1f8f16b"})
MERGE (:Property:Part:Internal {material: "GoreTex", percentage: ""})<-[:HAS_PROPERTY]-(n113)-[:HAS_PROPERTY]->(:Property:Part:Sole {material: "leather", percentage: ""})
MERGE (n113)-[:HAS_PROPERTY]->(:Property:Part:Heel {material: "rubber", percentage: ""})

```

it will result in the following graph:

 ![Screenshot 2025-02-21 alle 00.42.15](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/8/9/89555371997f0976eeee912de7076c23410327dc.png)

As you can see, from the Blue node ShoeBrand will start a 2 hops relationship chain named `HAS_CATEGORY`

it is

```auto
MATCH (c:Customer)->[ca1:HAS_CATEGORY]->(c1:Category)-[ca2:HAS_CATEGORY]->(c2:Category)-[pr:HAS_PRODUCT]->(p:Product)
return c,ca1,c1,ca2,c2,ps,p

```

the query

```auto
MATCH (c:Customer)-[ca1:HAS_CATEGORY]->(c1:Category)-[ca2:HAS_CATEGORY]->(c2:Category)-[pr:HAS_PRODUCT]->(p:Product)-[rpro:HAS_PROPERTY]->{1,}(pro:Property)
return c,ca1,c1,ca2,c2,pr,p,rpro,pro

```

returns only a partial graph, where there are some nodes that are detached from everything. Why?

 ![Screenshot 2025-02-21 alle 00.54.35](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/7/f/7fc876937b6dfdaedde825a8bd33d071a3d700de.png)

The second thing should be a little is more complicated:

How can I obtain to traverse the following query fragment

```auto
MATCH (c:Customer)-[ca1:HAS_CATEGORY]->(c1:Category)-[ca2:HAS_CATEGORY]->(c2:Category)-[pr:HAS_PRODUCT]->(p:Product)

```

using a variable length HAS\_CATEGORY relationship, and still returns all node and rels, something like

```auto
MATCH (c:Customer)-[ca1:HAS_CATEGORY*1..10]->(c1:Category {name: "Sneakers"})-[pr:HAS_PRODUCT]->(p:Product) return c,ca1,c1,pr,p

```

which doesn't return the Category chain between Customer and Product?

 ![Screenshot 2025-02-21 alle 01.06.30](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/9/4/94efca21548096ae9ce8d5867e595a2bb47632d4.png)

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [February 21, 2025, 4:21am UTC](https://community.neo4j.com/t/how-to-return-variable-length-pattern-in-a-quite-complex-db/72739/2 "2025-02-21T04:21:34Z")

</div>

The reason for the four pink nodes not showing connected to their related nodes, is that your variable-length pattern traverses through a "Color" node for these four nodes, and you have not variable that is bound to a Color nodes. As such, the Color node is not in the output, while the four pink nodes are.

You can rectify this in two ways. First, is to return the nodes and relationships of the paths, which will return all nodes (including the Color node).

```auto
MATCH path =(c:Customer)-[ca1:HAS_CATEGORY]->(c1:Category)-[ca2:HAS_CATEGORY]->(c2:Category)-[pr:HAS_PRODUCT]->(p:Product)-[rpro:HAS_PROPERTY]->{1,}(pro:Property)
return nodes(path), relationships(path)

```

 ![Screenshot 2025-02-20 at 11.08.18 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/9/d/9d95a63f12bbf5490b1e3f0ecb837658721e5e9c.png)

The second approach is to include the Color label in the node bound to variable "pro." It will be included in your results when you return "pro", as well as the Product nodes.

```auto
MATCH (c:Customer)-[ca1:HAS_CATEGORY]->(c1:Category)-[ca2:HAS_CATEGORY]->(c2:Category)-[pr:HAS_PRODUCT]->(p:Product)-[rpro:HAS_PROPERTY]->{1,}(pro:Color|Property)
return c,ca1,c1,ca2,c2,pr,p,rpro,pro

```

Which is better would depend on your requirements.

Your issue #2 is the result of the same cause as your issue #1. You are not returning all the nodes along the path, so you don't show the relationships. This makes the graph look disconnected. You can fix this by returning all the nodes and relationships along the paths.

```auto
MATCH path = (c:Customer)-[ca1:HAS_CATEGORY*1..10]->(c1:Category {name: "Sneakers"})-[pr:HAS_PRODUCT]->(p:Product) 
return nodes(path), relationships(path)

```

 ![Screenshot 2025-02-20 at 11.18.22 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/2/c/2cc95086b657e167632e01147eb8f75529065f1f.png)

Hope this helps...

---

<div class="post-metadata">

**Author:** ![paolodipietro58](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/paolodipietro58/32/21045_2.png) [@paolodipietro58](https://community.neo4j.com/u/paolodipietro58)\
**Post date:** [February 21, 2025, 8:12am UTC](https://community.neo4j.com/t/how-to-return-variable-length-pattern-in-a-quite-complex-db/72739/4 "2025-02-21T08:12:42Z")

</div>

Hi @glilienfield, happy to hear from you after a long time! How are you today?

Your latest suggestion solved the problem: you are always precise!

Thank you

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [February 21, 2025, 5:03pm UTC](https://community.neo4j.com/t/how-to-return-variable-length-pattern-in-a-quite-complex-db/72739/5 "2025-02-21T17:03:35Z")

</div>

Doing well. I was also excited to see a post by you. I miss working on your challenging and interesting problems.
