get common friends between 2 users and all mutual friends between user and common firends using neo4j

时间:2015-05-24 20:55:17

标签: neo4j cypher

Lets assume a,b,c and b,d,e and c,f,g, and a,e,g,h are friends .So

get mutual friends b/w a&b:

MATCH (me:User {username:'a'}) -[r:FRIENDS]-(mutualfriends) 
       -[r:FRIENDS]- (u:User {username:'b'}) 
RETURn mutualfriends

Now how to get all common friends from above and user 'a' ?
Also can we do all this in one query ?I really appreciate any help.

1 个答案:

答案 0 :(得分:3)

  1. Don't use r twice, otherwise you won't get the correct results for mutual-friends. Actually you don't need r at all.

you just continue to match.

MATCH (me:User {username:'a'})-[:FRIENDS]-(mf)-[:FRIENDS]-(u:User {username:'b'}) 
WITH distinct me, mf
MATCH (me)-[:FRIENDS]-(mmf)-[:FRIENDS]-(mf)
RETURN distinct mmf