Reputation: 61
I am trying to create relationship between retailers and products sold by the retailers. For example if the store "xyz" sells products item1, item2 and item3, I would like to create a relationship "SELLS_PRODUCTS" between them from store to the product.
Here is my sample code: Here Retailers is the retailers table, and Products is another table which has both the common details like the productname,storename etc which is required for the relationship.
MATCH (r:Retailers{Name: "Prestige Liquor Store"})
WITH r
MATCH (p:Products)
WHERE r.ProductproductName = p.ProductName
CREATE UNIQUE (r)-[:SELLS_PRODUCTS]->(p)
Upvotes: 1
Views: 2519
Reputation: 66989
Let's say each Retailer
node contains a productNames
list, and each Product
node has a name
. For example:
CREATE (:Retailer {name: 'Prestige Liquor Store', productNames: ['a', 'b', 'c']})
CREATE (:Product {name: 'b', price: 12.50}),
(:Product {name: 'c', price: 99.95});
Then this query would ensure that the "Prestige Liquor Store" has a unique SELLS
relationship to each of the Product
s it sells (that are in the DB):
MATCH (r:Retailer {name: "Prestige Liquor Store"})
MATCH (p:Product)
WHERE p.name IN r.productNames
MERGE (r)-[:SELLS]->(p);
Also, if you create indexes for :Retailer(name)
and Product(name)
, that should greatly speed up the above query if there are many retailers and/or products.
Upvotes: 5