Neo4j:找到连接度

Neo4j: find degree of connection

我正在使用 Neo4j 来查找用户之间的联系程度。我有以下形状的数据:

(user)-[:INTERACTS_WITH]->(user)

因此,如果 user_1 与 user_2 交互并且 user_2 与 user_3 交互,则 user_1 和 user_3 共享一个二级学位连接。

理想情况下,我想在 return 中获得如下数据集:

degree count
NULL   123
1      1050
2      3032
3      2110
...    ...

有没有比简单地为每对用户使用 运行 shortestPath() 函数更好的方法?如果不是,那么在 Neo4j 中遍历用户的最佳方式是什么?

此外,我想方向在这里起着一定的作用,所以你会建议使这种关系成为双向的,这样对于每个 (user1)-[:INTERACTS_WITH]->(user2) 我也会创建反向关系 (user2)-[:INTERACTS_WITH]->(user1)?

如果您对如何创建上述数据集有任何提示,请告诉我。

非常感谢!

Is there a better way to do this than to simply run shortestPath() function for every pair of users? If not, then what is the best way to loop over users in Neo4j?

我相信运行shortestPath()对每一对用户来说都是不错的选择,但请记住,它应该很贵。

Also, I imagine the direction plays a role here, so would you recommend making this relationship bidirectional, so that for every (user1)-[:INTERACTS_WITH]->(user2) I would also create the reverse relationship (user2)-[:INTERACTS_WITH]->(user1)?

不,你不需要另一种关系。请记住,在 Neo4j 中查询时可以忽略关系方向。当对自然双向的关系建模时,我们应该只使用一个关系来表示它。因此,在查询图形时,我们可以从 a 横向到 bba。当双向关系中的某些数据在 abba 之间可能不同时,您只需要一个额外的关系。假设你的模型中用户之间的交互有一个权重,这个权重可以从 abba 不等。在这种情况下,您可以将此权重存储为关系中的 属性。示例:

(a)-[:INTERACTS_WITH {weight:10}]->(b)
(b)-[:INTERACTS_WITH {weight:6}]->(a)

查看 this link 中有关建模双向关系的内容。