7.5. Basic friend finding based on social neighborhood

Imagine an example graph like the following one:

Figure 7.4. Graph


To find out the friends of Joe’s friends that are not already his friends, the query looks like this:

Query. 

START joe=node:node_auto_index(name = "Joe")
MATCH joe-[:knows*2..2]-friend_of_friend
WHERE not(joe-[:knows]-friend_of_friend)
RETURN friend_of_friend.name, COUNT(*)
ORDER BY COUNT(*) DESC, friend_of_friend.name

This returns a list of friends-of-friends ordered by the number of connections to them, and secondly by their name.

Result

friend_of_friend.nameCOUNT(*)
3 rows

"Ian"

2

"Derrick"

1

"Jill"

1


Try this query live. (1) {"name":"Bill"} (2) {"name":"Sara"} (3) {"name":"Derrick"} (4) {"name":"Ian"} (5) {"name":"Jill"} (6) {"name":"Joe"} (1)-[:knows]->(3) {} (1)-[:knows]->(4) {} (2)-[:knows]->(1) {} (2)-[:knows]->(4) {} (2)-[:knows]->(5) {} (6)-[:knows]->(1) {} (6)-[:knows]->(2) {} start joe=node:node_auto_index(name = "Joe") match joe-[:knows*2..2]-friend_of_friend where not(joe-[:knows]-friend_of_friend) return friend_of_friend.name, COUNT(*) order by COUNT(*) DESC, friend_of_friend.name