How to return variable length pattern in a quite complex DB

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

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:

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

it is

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

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?

The second thing should be a little is more complicated:

How can I obtain to traverse the following query fragment

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

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?

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).

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)

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.

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.

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

Hope this helps...

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

1 Like

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