简体   繁体   English

Neo4j cypher。 设置节点之间的反向关系

[英]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.

相关问题 neo4j cypher节点之间的多种关系 - neo4j cypher multi relationship between nodes Neo4j-Cypher:通过Cypher设置显示参数。 - Neo4j-Cypher : Setting display parameters through Cypher. 像LinkedIn这样的网站如何有效显示用户之间的第一,第二和第三距离。 我们可以用neo4j cypher做同样的事情吗? - How do sites like LinkedIn efficiently show the relationship distance 1st,2nd and 3rd between users. can we do the same with neo4j cypher.? Cypher Neo4j在两个节点之间创建关系并避免双向关系 - Cypher Neo4j creating Relationship between two nodes and avoiding bidirectional relationship 将节点与具有常见关系的公共节点匹配-Neo4j Cypher - Match nodes with common nodes with a relationship - Neo4j Cypher 在neo4j中创建节点之间的关系 - Create relationship between nodes in neo4j 无法使用Java中的Cypher查询在Neo4J数据库中的两个节点之间创建关系 - Unable to create relationship between two nodes in Neo4J database using Cypher query in Java Neo4j / Cypher:返回节点本身内节点之间关系的值之和 - Neo4j / Cypher: Returning sum of value in relationship between nodes within the node itself 如何使用cypher在Neo4j中给出它们的属性来创建两个节点之间的关系 - How to use cypher to create relationship between two nodes given their attributes in Neo4j Neo4j密码查询:具有指定节点和关系属性的AllShortestPaths - Neo4j cypher query: AllShortestPaths with specified nodes and relationship properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM