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