Reputation: 2487
I'm working on connecting nodes with the same label values to show relationships. However, when I run the code below, it's doing so, but also adding a relationship to itself as well as the other node with same name:
MATCH (a:rec_sku), (b:rec_sku)
WHERE a.also_bought_sku = b.also_bought_sku
CREATE (a)-[r:SAME_SKU]->(b)
I also tried a more complex solution using nested loops, but ran into the same issue:
MATCH (n:rec_sku)
WITH collect(n) as plist
FOREACH (x IN plist |
FOREACH (y IN filter(z in plist WHERE x.also_bought_sku = z.also_bought_sku) |
CREATE (y)-[:SAME_SKU]->(x)
)
);
Upvotes: 0
Views: 1667
Reputation: 8546
To avoid adding a self-relationship, just add a WHERE
clause to filter:
MATCH (a:rec_sku), (b:rec_sku)
WHERE NOT a=b AND a.also_bought_sku = b.also_bought_sku
CREATE UNIQUE (a)-[r:SAME_SKU]->(b)
You can also use the CREATE UNIQUE
statement to ensure no duplicate relationships are created.
Upvotes: 1