Cypher to take all relationships from one node and attach them to another node

My question is: are two nodes asked, node A and node B, is there a cypher request that removes all relations from node A and binds them instead of node B, so that all nodes that were previously bound to node A are now bound to node B?

I am sure that for such a request there are many use cases, but my use case is to combine several logins on social networks:

Given that I have a member account (member1) using Google as a sign in the provider And I have a separate member account (member2) using facebook as a sign in the provider When member1 tries to connect to the same facebook account that member2 uses as a sign in the provider AND member1 asks for a merge (account merge) And member2 confirms the merge Then the accounts for member1 and member2 will be merged And one member will use both google and facebook as a signin provider.

What is a cypher request for this?

+3
source share
2 answers

EDIT: There is now a way to dynamically indicate the type of relationship. Thus, the workaround below is limited to relationships with the same type and without properties.


Let me create some data:

CREATE (n1:Node1) CREATE (n2:Node2) CREATE (target1:Target) CREATE (target2:Target) CREATE (target3:Target) CREATE (n1)-[:REL]->(target1) CREATE (n1)-[:REL]->(target2) CREATE (n1)-[:REL]->(target3); 

We have such data right now:

data

Now we want to β€œmove” the relation from Node1 to Node2

Query:

 MATCH (n1:Node1)-[r:REL]->(target) MATCH (n2:Node2) CREATE (n2)-[:REL]->(target) DELETE r 

And the result:

result

What we mainly do:

  • Get connected in one way
  • Create relationships with other node
  • Remove relationship from original node
+5
source

Below is an alternative solution code. I am having problems running unit tests, but as soon as they are fixed, I will post a mini-project for this on github.

 import org.neo4j.graphdb.Direction; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Relationship; import org.neo4j.graphdb.Transaction; import org.neo4j.server.plugins.Description; import org.neo4j.server.plugins.Name; import org.neo4j.server.plugins.Parameter; import org.neo4j.server.plugins.PluginTarget; import org.neo4j.server.plugins.ServerPlugin; import org.neo4j.server.plugins.Source; /** * @author John Deverall * This plugin transfers all relationships from one node to another, including the relationships * direction, type and properties. * Can be called from the neo4j browser ui with command * :POST /db/data/ext/TransferRelationships/graphdb/transfer_relationships {"sourceNode":"/db/data/node/<node_id>", "destinationNode":"/db/data/node/<node_id>"} */ public class TransferRelationships extends ServerPlugin { @Name("transfer_relationships") @Description("Transfer all relationships from one node to another") @PluginTarget(GraphDatabaseService.class) public Long transferNodes(@Source GraphDatabaseService graphDb, @Description("The source node") @Parameter(name = "sourceNode", optional = false) Node sourceNode, @Description("The destination node") @Parameter(name = "destinationNode", optional = false) Node destinationNode) { try (Transaction tx = graphDb.beginTx()) { // transfer outgoing relationships Iterable<Relationship> outgoingRelationships = sourceNode.getRelationships(Direction.OUTGOING); for (Relationship relationship : outgoingRelationships) { Node otherNode = relationship.getOtherNode(sourceNode); Relationship newRelationship = destinationNode.createRelationshipTo(otherNode, relationship.getType()); Iterable<String> propertyKeys = relationship.getPropertyKeys(); for (String key : propertyKeys) { Object property = relationship.getProperty(key); newRelationship.setProperty(key, property); } relationship.delete(); } // transfer incoming relationships Iterable<Relationship> incomingRelationships = sourceNode.getRelationships(Direction.INCOMING); for (Relationship relationship : incomingRelationships) { Node otherNode = relationship.getOtherNode(sourceNode); Relationship newRelationship = otherNode.createRelationshipTo(destinationNode, relationship.getType()); Iterable<String> propertyKeys = relationship.getPropertyKeys(); for (String key : propertyKeys) { Object property = relationship.getProperty(key); newRelationship.setProperty(key, property); } relationship.delete(); } tx.success(); } return destinationNode.getId(); } } 
0
source

Source: https://habr.com/ru/post/981111/


All Articles