简体   繁体   中英

Neo4j cypher. setting reverse relationship between nodes

I have defined all relationships r with a property reverse relationship rr .

I am able to recover all relationship types and their rr with a cypher query.

Now I wish to iterate over each relation type(r) , get all pairs of nodes, which have this relationship. Say (a)->[type(r)]-(b) . Then create reverse relation ship create (b)-[rr]->(a) if it does not exist.

New to 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 . 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:

MATCH (p:Person {id: 123})-[:HAS_ROOMMATE]-(q)
RETURN p, q

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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