Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
10-05-2022 09:20 AM
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:
ARROW MODEL:
Solved! Go to Solution.
10-05-2022 11:41 AM
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:
Scenario 2:
10-05-2022 10:28 AM
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 !
10-05-2022 10:56 AM
Would adding the Product IDs as a property of Order nodes allow me to link the Product nodes?
10-05-2022 11:58 AM - edited 10-05-2022 11:59 AM
Just at runtime with cypher query , then create the "physical" relations as the same way in answers below.... depends on your business needs
10-05-2022 11:22 AM
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
10-05-2022 11:41 AM
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:
Scenario 2:
10-05-2022 12:00 PM
This worked! Thank you so much, I really appreciate your help.
All the sessions of the conference are now available online