0
votes

I have created 5 nodes in neo4j as follows.

Node 1 {userid:1000, username: A, someOtherProperties...}
Node 2 {userid:2000, username: B, someOtherProperties...}
Node 3 {userid:3000, username: c, someOtherProperties...} 
Node 4 {userid:4000, username: D, someOtherProperties...} 
Node 5 {userid:5000, username: E, someOtherProperties...} 

Node 1 connected with Node 2 & 3, and Node 2 connected with node 1, 3, 4

1 -> 2
1 -> 3
2 -> 1
2 -> 3
2 -> 4
3 -> 4

Now I want user suggestion for node 1 which contain those node which is not connected with it self with mutual count. I want result like this.

node id  userid  username  mutual count
-------  ------  --------  -------------
4    4000    D         2             (which is node 2 & 3)
5    5000    E         0    

I had tried cypher query, but I didn't get success.

1
What query did you try? Can you share that?Luanne
Thanks @Luanne. I had tried following query : START user=node:node_auto_index("userid:*"), me=node:node_auto_index(userid = '92') WHERE user <> me WITH user MATCH pMutualFriends=me-[:friends]->mf<-[?:friends]-user RETURN user.userid, user.UserName, COUNT(pMutualFriends) As MutualCount Order by MutualCount desc; I had forgotten to put '?' in relationship before, that I had corrected. I have got result what I expected, But I am not sure. Is it correct way or not?Manish Sapkal
So you are looking to for a user U, find all friends of friends U and the number of U's friends connected to the friends-of-friends? Not sure why E turns up in your example because no friends are connected to E. Maybe I misunderstood the purpose of your query?Luanne
No @Luanne. I want E also. I want all users, but It should be ordered by mutualcount maximum to minimum. I just want to show No Of Mutual friends between logged in user and other user. so, logged in user can sent invitation to that user. If new user can not have any mutual contact, though he/she can get suggestion as per his/her city or country. and one more thing, I have to show random 10 records. thank you.Manish Sapkal
Okay, if that's what you want then you're query is fine. Note that however it could really be expensive because you are matching all user nodesLuanne

1 Answers

0
votes

Please try

START user=node:node_auto_index(name='A'), f=node(*) 
MATCH user-[r?:FRIEND*1..2]->(f) 
WITH DISTINCT r AS friendRelation,f 
RETURN count(friendRelation),f

Which will give you the number of friend relations to every other node with a depth 2 (friend of a friend)