Neo4j:查找连接程度 [英] Neo4j: find degree of connection

查看:87
本文介绍了Neo4j:查找连接程度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Neo4j查找用户之间的连接程度.我有以下关注者形状的数据:

I'm using Neo4j to find the degree of connection between users. I have data in the follower shape:

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

因此,如果user_1与user_2交互,而user_2与user_3交互,则user_1和user_3共享二级连接.

So if user_1 interact with user_2 and user_2 interacts with user_3, then user_1 and user_3 share a second-degree connection.

理想情况下,我希望得到如下这样的数据集:

Ideally, I would like to get the following dataset like this in return:

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

是否有比单独为每对用户运行shortestPath()函数更好的方法?如果不是,那么在Neo4j中环回用户的最佳方法是什么?

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?

此外,我想方向在这里也起着作用,所以您会建议将此关系设为双向,以便对于每个(user1)-[:INTERACTS_WITH]->(user2)我也会创建反向关系(user2)-[:INTERACTS_WITH]->(user1)?

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)?

如果您对如何创建上述数据集有任何建议,请告诉我.

If you have any tips on how to create the above dataset, please let me know.

非常感谢!

推荐答案

有没有比仅运行shortestPath()更好的方法了? 对每对用户起作用?如果没有,那么什么是最好的方法 循环Neo4j中的用户?

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()是一个不错的选择,但是请记住,它应该非常昂贵.

I believe that run shortestPath() for every pair of users is a good choice, but keep in mind that it should be very expensive.

我还想说方向在这里起作用,所以你会 建议将此关系设为双向,这样对于每个 (user1)-[:INTERACTS_WITH]->(user2)我也会创建相反的 关系(user2)-[:INTERACTS_WITH]->(user1)?

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中,在查询时可以忽略关系方向.在为自然双向关系建模时,我们应仅使用一种关系来表示它.因此,在查询图形时,我们可以从ab,从ba.仅当abba的双向关系中的某些数据不同时,才需要额外的关系.假设模型中用户之间的交互具有权重,并且该权重可以从abba不同.在这种情况下,您可以将此权重存储为关系中的属性.示例:

No, you do not need another relationship. Remember that relationship direction can be ignored at query time in Neo4j. When modeling relationships that are naturally bidirectional we should use only one relationship to represent it. So when querying the graph we can transverse from a to b and b to a. You only need an extra relationship when some data in the bidirectional relationship can be different between a to b and b to a. Suppose that the interaction between the users in your model has a weight and this weight can be different from a to b and b to a. In this case you can store this weight as a property in the relationship. Example:

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

此链接中了解一下双向关系建模.

Take a look in this link about modelling bidirectional relationships.

这篇关于Neo4j:查找连接程度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆