neo4j - user suggestion with mutual count

2019-09-13 21:47发布

问题:

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:

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)