apoc.refactor.mergeNodes`: Caused by: org.neo4j.graphdb.NotFoundException: Node 912779 not found

In a database with genealogical informations i try to merge nodes of persons which belong to the same person. They are connected with "SAME_AS" edges. When i try this query:

MATCH (n1:Person)-[s1:SAME_AS]-(n2:Person)
WHERE n1.properties =~ '[A-Z][1-9].*'
AND n1.age = n2.age
WITH collect([n2, n1]) as listOfNodes LIMIT 100000
UNWIND listOfNodes as nodes
call apoc.refactor.mergeNodes(nodes) yield node return count(*);
return nodes;

I get this error message:

Neo.ClientError.Procedure.ProcedureCallFailed: Failed to invoke procedure `apoc.refactor.mergeNodes`: Caused by: org.neo4j.graphdb.NotFoundException: Node 912779 not found

Here is the code to create the test graph:


Is it possible for your data to have the same node related to more than one node, so that the time it is trying to merge it for the second time, it has been already merged and does not exists.

Could be ... haven't thought about that possibility yet.

I see in your data you have a few pair of nodes that have a bi-directional SAME_AS relationship, so you would have elements [n1, n2] and [n2, n1] in your listOfNodes. I would imagine after the first element [n1, n2] gets merged, then you would get the node-not-found error when merging [n2, n1].

Yes, but how can i organize this ...

You can delete one of the bidirectional relationships before your merge query. The following should find the pairs that have bi-directional relationships and delete one of them. The query found 8 of them.

delete r2

I found 78 relationships, 8 being bidirectional. After the delete, there are 70 relationships and I did not see any bi-directional pairs in the graph.

Thats the point - thank you ;-)

Did that solve your problem?

Unfortunately not because the bigger picture is more complicated. But your notes made the problems clear for me and I'm working on it.