繁体   English   中英

如何计算与 neo4j 的共同好友?

[英]How to calculate mutual friends with neo4j?

我想使用 neo4j 来管理用户之间的关系。

我怎样才能让共同的朋友使用它?

在使用 cypher 的情况下,以下查询返回共同的朋友:

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

上面的示例返回节点 1 和 4 的共同朋友

在此处输入图像描述

以下是图片中示例的查询副本及其结果:

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

最简单的方法是跨 FRIEND_OF 关系对两个用户使用长度为 2 的最短路径算法。 这些是通过一个朋友跳连接两个用户的路径。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM