Imagine an example graph like the following one:
To find out the friends of Joe’s friends that are not already his friends, the query looks like this:
Query
MATCH (joe { name: '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.name | COUNT(*) |
---|---|
3 rows | |
|
|
|
|
|
|
Try this query live create (_0 {`name`:"Joe"}) create (_1 {`name`:"Ian"}) create (_2 {`name`:"Bill"}) create (_3 {`name`:"Sara"}) create (_4 {`name`:"Jill"}) create (_5 {`name`:"Derrick"}) create (_0)-[:`knows`]->(_3) create (_0)-[:`knows`]->(_2) create (_2)-[:`knows`]->(_1) create (_2)-[:`knows`]->(_5) create (_3)-[:`knows`]->(_4) create (_3)-[:`knows`]->(_1) create (_3)-[:`knows`]->(_2) ; match (joe {name: '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