Reputation: 45
Here is my query:
MATCH (a:Person)-[:friend]->(p:Person)<-[:friend]-(b:Person)
WITH a, b, COUNT(p) as count
WHERE count >= 2
CREATE (a)-[:friend {new: "yes"}]->(b)
RETURN a,b,count
I tried to find a and b that have at least 2 common friends and creating friend relationship between them. Here is the result sample:
a b count
name1 name2 3
name2 name1 3
Notes that the result is repeated, and this will create a bi-directional relationship between them. name1-[:friend]->name2 also means name1<-[:friend]-name2, so if I created a bi-directional relationship, the relationship between them will be duplicated as shown in my csv file:
a b relationship
name1 name2 friend
name1 name2 friend
name2 name2 friend
name2 name2 friend
Is there any way I can avoid this? Thank you for your time.
Upvotes: 2
Views: 1206
Reputation: 10856
Yeah, there's a trick to do this:
MATCH (a:Person)-[:friend]->(p:Person)<-[:friend]-(b:Person)
WHERE ID(a) < ID(b)
WITH a, b, COUNT(p) as count
WHERE count >= 2
CREATE (a)-[:friend {new: "yes"}]->(b)
RETURN a,b,count
The change is the WHERE ID(a) < ID(b)
which makes sure that just one of the two directions is chosen
Upvotes: 5