cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

How to connect products within same order?

mkwoodruff
Node Link

Hello,

Attached is the schema of a graph for a retail sales dataset. I would like to create relationships between products that are in the same order. I've also attached an Arrow model of what I'm trying to do - see [:BOUGHT_WITH]. I've tried a few things but nothing has worked. Any help would be appreciated, thanks so much!

SCHEMA:

Screen Shot 2022-10-05 at 11.14.57 AM.png

ARROW MODEL:

Screen Shot 2022-10-05 at 11.13.52 AM.png

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

This seems to work:

match(n:Order)-[:ORDER_CONTAINS]->(p:Product)
with n, collect(p) as products
unwind products as prod1
unwind products as prod2
with prod1, prod2
where id(prod1) < id(prod2)
merge(prod1)-[:BOUGHT_WITH]->(prod2)

Scenario 1:

Screen Shot 2022-10-05 at 2.35.16 PM.png

Screen Shot 2022-10-05 at 2.35.39 PM.png

Scenario 2:

Screen Shot 2022-10-05 at 2.37.57 PM.png

Screen Shot 2022-10-05 at 2.39.28 PM.png

 

View solution in original post

6 REPLIES 6

ponceortiz
Node Clone

It looks like you should have a property on the Order node with each Product ID in a property of type List, assuming each Order points to the Product Catalog, or use an intermediate node " Item" that links

Order->Item->Product , I  like this because maintenance of the Product catalog, You can use more properties in Item, quantity , unit price , total amount , date, etc   So it depends in your businnes use case

Saludos !

Would adding the Product IDs as a property of Order nodes allow me to link the Product nodes? 

Just at runtime with cypher query , then create the "physical" relations as the same way in answers  below....  depends on your business needs 

ameyasoft
Graph Maven

Try this:
MATCH (a:OrderID)-[:ORDER_CONTAINS}->(b:Product)
WHERE a.orderID = 123
WITH b order by id(b) ASC
WITH collect(b) as prods
CALL apoc.nodes.link(prods, 'BOUGHT_WITH')
RETURN prods

glilienfield
Ninja
Ninja

This seems to work:

match(n:Order)-[:ORDER_CONTAINS]->(p:Product)
with n, collect(p) as products
unwind products as prod1
unwind products as prod2
with prod1, prod2
where id(prod1) < id(prod2)
merge(prod1)-[:BOUGHT_WITH]->(prod2)

Scenario 1:

Screen Shot 2022-10-05 at 2.35.16 PM.png

Screen Shot 2022-10-05 at 2.35.39 PM.png

Scenario 2:

Screen Shot 2022-10-05 at 2.37.57 PM.png

Screen Shot 2022-10-05 at 2.39.28 PM.png

 

This worked! Thank you so much, I really appreciate your help.