开发者

How to calculate mutual friends with neo4j?

开发者 https://www.devze.com 2023-03-12 04:02 出处:网络
I want to use neo4j to manage relationship among users. How can I ge开发者_如何学运维t mutual friends using it?The easiest way would be to use the shortest path algorithm of length 2, with the two us

I want to use neo4j to manage relationship among users.

How can I ge开发者_如何学运维t mutual friends using it?


The easiest way would be to use the shortest path algorithm of length 2, with the two users, across FRIEND_OF relationships. Those are the paths that connect the two users via exactly one friend hop.

PathFinder<Path> finder = GraphAlgoFactory.shortestPath(
        Traversal.expanderForTypes( FRIEND_OF ), 2 );
Iterable<Path> paths = finder.findAllPaths( user1, user2 );


In case of using cypher the following query returns mutual friends:

start a = node(1), b = node(4) match (a)--(x)--(b) return x;

The example above returns mutual friends of node 1 and 4

How to calculate mutual friends with neo4j?

Below is the copy of queries and its results for the example from the picture:

neo4j-sh (0)$ start a = node(1), b = node(4) match (a)--(x)--(b) return x;
==> +--------------------+
==> | x                  |
==> +--------------------+
==> | Node[3]{Name->"C"} |
==> +--------------------+
==> 1 row
==> 9 ms
==> 
neo4j-sh (0)$ start a = node(1), b = node(6) match (a)--(x)--(b) return x;
==> +--------------------+
==> | x                  |
==> +--------------------+
==> | Node[5]{Name->"E"} |
==> | Node[2]{Name->"B"} |
==> +--------------------+
==> 2 rows
==> 0 ms
0

精彩评论

暂无评论...
验证码 换一张
取 消