Neo4j merge 2 or more duplicate nodes

I feed my neo4j db manually using cypher, so I am prone to errors, for example, by creating duplicate nodes:

Duplicated nodes will have all relationships with other nodes. Is there a built-in function to combine these nodes? Or should I do it manually?

Sounds are possible, but complex with a cypher script:

    • Get the relationship of each duplicate node
    1. Restore them (with your properties) with the correct node (given node id)
  1. Delete relationships with duplicate nodes
  1. and finally remove the duplicated nodes.
+6
source share
2 answers

To avoid this situation in the future, browse the MERGE keyword in Cypher. Unfortunately, as far as I know, there is nothing like this in Cypher (yet):

MATCH (n:MyNode),(m:MyNode) WHERE ID(n) <> ID(m) AND PROPS(n) IN PROPS(m) AND PROPS(m) IN PROPS(n) (...) DELETE (...) 

The fictional third-line PROPS function is not part of the Cypher language, and custom functions have not yet entered Neo4j.

If you are not working with production instances, the easiest way is to create a backup copy of the data folder and try to start it (using MERGE).

Otherwise, you can also write a workaround to collect duplicates and remove them in batch mode (here is an example using the REST API).

+2
source

Try the following:

 MATCH (n:MyNode),(m:MyNode),(o:OtherNode {id:123}) WHERE n <> m MATCH (m)-[r:FOO]->() CREATE (n)-[r2:FOO]->(o) SET r2 = r DELETE r,m 
+2
source

All Articles