Search code examples
neo4jduplicatescypherneo4j-apoc

Neo4j Cypher merge nodes and persist


I'm trying to merge duplicate nodes in my database, they have some shared properties but are not the same. I'm finding them and merging it correctly, but there is no change to the database.

This is my query:

MATCH (root:Person { firstName: "X", lastName: "Y" }),(p)
WHERE root.firstName = p.firstName AND root.lastName = p.lastName
AND root.network = "D" AND p.network = "D"
WITH head(collect([root,p])) as nodes
CALL apoc.refactor.mergeNodes(nodes,{
properties:'combine',
mergeRels:true
})
YIELD node
return node

I was expecting this to replace the original (2) nodes with this new node, however all this does is return the node and there is no change to the underlying data.


Solution

  • I figured this out - it does actually appear to update the database. The issue I had was the MATCH statement created a cartesian join - and when I call HEAD it only takes the first element, which is the first node and itself.

    By adding the following line: AND id(root) <> id(p) (in the WHERE statement of course) it prevents this behaviour and works as intended.