[英]Neo4j cypher. setting reverse relationship between nodes
I have defined all relationships r
with a property reverse relationship rr
.我已经使用属性反向关系rr
定义了所有关系r
。
I am able to recover all relationship types and their rr
with a cypher query.我可以使用 cypher 查询恢复所有关系类型及其rr
。
Now I wish to iterate over each relation type(r)
, get all pairs of nodes, which have this relationship.现在我希望遍历每个关系type(r)
,获取所有具有这种关系的节点对。 Say (a)->[type(r)]-(b)
.说(a)->[type(r)]-(b)
。 Then create reverse relation ship create (b)-[rr]->(a)
if it does not exist.然后创建反向关系 create (b)-[rr]->(a)
如果它不存在。
New to cypher. cypher 的新手。 Unable to make headway.无法取得进展。
please help请帮忙
What you are asking about appears to be a bad idea.你所问的似乎是一个坏主意。
Enforcing a pattern where a relationship of some given type is always paired with another relationship of some given type in the opposite direction (and vice versa) is bad practice .强制执行一种模式,其中某种给定类型的关系总是与某种给定类型的另一种关系以相反的方向配对(反之亦然)是不好的做法。 This is because, with neo4j, following a relationship in either direction is equally easy and efficient .这是因为,对于 neo4j,沿任一方向跟踪关系同样容易且有效。 It is wasteful to create relationships when not necessary.在不必要的时候建立关系是一种浪费。
Sample anti-patterns :示例反模式:
(a)-[:HAS_ROOMMATE]->(b)-[:HAS_ROOMMATE]->(a)
and also:并且:
(a)-[:HAS_HUSBAND]->(b)-[:HAS_WIFE]->(a)
On the other hand, if one cannot assume that a relationship in one direction must imply some relationship in the opposite direction, then it may be OK to have relationships in opposite directions (but you should also consider the alternative of adding an appropriate flag property to a single relationship).另一方面,如果不能假设一个方向上的关系必须暗示相反方向上的某种关系,那么建立相反方向的关系可能是可以的(但您还应该考虑将适当的标志属性添加到单一关系)。
By the way, when querying for a relationship, you can specify a non-directional pattern.顺便说一句,在查询关系时,您可以指定非定向模式。 For example, to find a Person
's roommate(s) you can do this (notice that the relationship pattern does not specify an arrowhead in either direction:例如,要查找Person
的室友,您可以这样做(请注意,关系模式未指定任一方向的箭头:
MATCH (p:Person {id: 123})-[:HAS_ROOMMATE]-(q)
RETURN p, q
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.