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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.