Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neo4j change relationship target from one node to another

Assume I have this model in neo4j:

          (n2) -> (n3)-> (n9)
           /\
           |
   (n4)<-(n1)->(n5)->(n6)
    |            |
    \/           \/ 
    (n7)         (n8)

I need a cypher to change relationship between (n1) and (n2) to (n1) and (n6), like this:

                     (n2) -> (n3)-> (n9)
                      /\
                      |
   (n4)<-(n1)->(n5)->(n6)
    |            |
    \/           \/ 
    (n7)         (n8)
like image 684
Arash Mousavi Avatar asked May 06 '17 07:05

Arash Mousavi


People also ask

How do you create a relationship between existing nodes in Neo4j?

To create a relationship between two nodes, we first get the two nodes. Once the nodes are loaded, we simply create a relationship between them. The created relationship is returned by the query.

Can u relabel node in Neo4j?

You can change the nodes associated with a label but you can't change the type of a relationship.

How do I change my relationship name in Neo4j?

You cannot rename an already existing relationship. You'd have to run through all relationships, create the new one in parallel (including all properties) and then remove the old one.

Is Neo4j bidirectional?

Neo4j cannot store bidirectional relationships. No way around this, you can however treat relationships as bidirectional when querying your graph.


Video Answer


2 Answers

For those without APOC, Here is how to copy the relationship

MATCH (n1)-[r1:foo]->(n2),(n6)
WHERE n1.id = 1 AND n2.id = 2 and n6.id = 6
CREATE (n2)-[r2:foo]->(n6)
SET r2=r1
DELETE r1
like image 181
Tezra Avatar answered Nov 15 '22 10:11

Tezra


If you have APOC Procedures installed, you can use a graph refactoring procedure to change the end point of a relationship. You'll need a match query to get the relationship, and the new start node, then use:

call apoc.refactor.from(rel, newStartNode)
like image 20
InverseFalcon Avatar answered Nov 15 '22 10:11

InverseFalcon