简体   繁体   English

Neo4j数据建模查询

[英]Neo4j data modeling query

I am working on a project (platform) where I am using neo4j to make connections between users. 我正在使用Neo4j建立用户之间的连接的项目(平台)。 There are following different ways where user can connect to other users on the platform. 用户可以通过以下几种不同的方式连接到平台上的其他用户。

  1. When a user logs in with Facebook, I get his mutual friends who are already using our platform. 当用户使用Facebook登录时,我得到了他的共同朋友,他们已经在使用我们的平台。 There I make a connection in my graph database by creating a new node (current user) and make connections with all existing nodes whom he knows through facebook. 在那里,我通过创建一个新节点(当前用户)在我的图形数据库中建立连接,并与他通过facebook认识的所有现有节点建立连接。

  2. One user also connects with other user if both are staying in same society/community. 如果两个用户都住在同一个社会/社区,则一个用户还会与其他用户建立联系。 So the use case is, once a user updates his residential address (society name, city) than I make a query in graph db and get all nodes who also stay in the same society and create this new user with those nodes with relationship name "Same society". 因此,用例是,一旦用户更新了其住所地址(社会名称,城市),然后我在图db中进行查询,并获得所有仍处于同一社会的节点,并使用关系名称为“同一个社会”。

  3. Same was user might be connected with other user if both users study in same college or school. 如果两个用户都在同一所大学或学校学习,则该用户可能已与其他用户建立联系。 I make a connection between two nodes with relationship "Same college/school". 我在两个节点之间建立了联系,关系为“同一所大学/学校”。

What is the best way to model the above problem in neo4j? 在neo4j中为上述问题建模的最佳方法是什么? If I do a query in DB to get all the relationship types and shortest path of all the relationships between given two nodes, which model will be optimized for this type of query? 如果我在数据库中执行查询以获取给定两个节点之间的所有关系类型和所有关系的最短路径,那么将针对该类型的查询优化哪种模型?

  1. Simply create a relationship between users 只需在用户之间建立关系

    (user)-[:FRIEND_OF]-(user)

    Relationships in neo4j are always directional, but you can ignore the direction because, in this case, it doesn't make much sense. neo4j中的关系始终是定向的,但是您可以忽略方向,因为在这种情况下,它没有多大意义。 There is no performance penalty for traversing a relationship "backwards". 对于“向后”遍历关系不存在性能损失。

  2. Create a node representing your society/community and link all users living there to that node. 创建一个代表您的社会/社区的节点,并将生活在那里的所有用户链接到该节点。 This has several benefits: 这有几个好处:

    • when you add new user to a community you only create one relationship (compared to linking the user directly to all users, which is N relationships), same goes for removing user from community. 当您将新用户添加到社区时,您只能创建一个关系(与将用户直接链接到所有用户(N个关系)相比),从社区中删除用户也是如此。

    • you can create different relationships between the user and community 您可以在用户和社区之间创建不同的关系

    (user)-[:LIVES]-(community1) (user)-[:WORKS]-(community2)

  3. This is essentially the same as no. 这基本上与否相同。 2. You can differentiate between types of communities (schools, etc..) by having different labels/properties. 2.您可以通过具有不同的标签/属性来区分社区(学校等)的类型。

Finding a shortest path between two nodes in this model will give you their closest connection, the kind of "know via something". 在此模型中,找到两个节点之间的最短路径将为您提供最紧密的联系,即“通过某些事物知道”。 You may limit the path by relationship type/community type etc. 您可以通过关系类型/社区类型等限制路径。

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

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