简体   繁体   English

Neo4j双向关系

[英]Neo4j Bidirectional Relationship

Is there a way to create bidirectional relationship in Neo4j using Cypher? 有没有办法使用Cypher在Neo4j中创建双向关系? I would like the relationship to be bidirectional rather than making two unidirectional relationships in both directions For eg: 我希望这种关系是双向的,而不是在两个方向上形成两个单向关系,例如:

(A)<-[FRIEND]->(B)

Rather than: 而不是:

(A)-[FRIEND]->(B)
(A)<-[FRIEND]-(B)

Thanks in advance :) 提前致谢 :)

No, there isn't. 不,没有。 All relationships in neo4j have a direction, starting and ending at a given node. neo4j中的所有关系都有一个方向,从给定节点开始和结束。

There are a small number of workarounds. 有一些解决方法。

  • Firstly, as you've suggested, we can either have two relationships, one going from A to B and the other from B to A. 首先,正如你所建议的那样,我们可以有两种关系,一种是从A到B,另一种是从B到A.

  • Alternatively, when writing our MATCH query, we can specify to match patterns directionlessly, by using a query such as 或者,在编写MATCH查询时,我们可以通过使用诸如的查询来指定无方向匹配模式

     MATCH (A)-[FRIEND]-(B) RETURN A, B 

    which will not care about whether A is friends with B or vice versa, and allows us to choose a direction arbitrarily when we create the relationship. 这不关心A是否是B的朋友,反之亦然,并允许我们在创建关系时任意选择方向。

According to this article: Modeling Data in Neo4j: Bidirectional Relationships 根据这篇文章: 在Neo4j中建模数据:双向关系

The strictly better choice is to create a relationship in an arbitrary direction and not specify the direction when querying: 绝对更好的选择是在任意方向创建关系,而不是在查询时指定方向:

MATCH (neo)-[:PARTNER]-(partner)

The engine is capable of traversing the edge in either direction. 发动机能够沿任一方向穿过边缘。 Creating the anti-directional edge is unnecessary and only serves to waste space and traversal time. 创建反方向边缘是不必要的,仅用于浪费空间和遍历时间。

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

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