Merging nodes from CSV

I have a long list of uuids for nodes I know to be the same stored in a csv (uuid1 and uuid2). When I use the following simple query, it only yields the result for the first row and does not continue on with the rest of the sheet. Any ideas?

LOAD CSV WITH HEADERS FROM 'file:///XXX.csv' AS row
MATCH (a1:Person), (a2:Person)
WHERE a1.uuid = row.uuid1 AND a2.uuid = row.uuid2
WITH head(collect([a1,a2])) as nodes
CALL apoc.refactor.mergeNodes(nodes,{
properties: "discard",
mergeRels:true
})
YIELD node
RETURN node

Hello @mkretsch :slight_smile:

Can you share the CSV file here please? (you must replace the .csv extension by .txt).

Regards,
Cobra

@mkretsch

It should depend on the csv, but if you want to merge (a1:Person) and (a2:Person),
I think you have to remove head and collect functions.
With your query, you group every result into the collect and with head you pick only the 1st result.


Instead, this query should work:

LOAD CSV WITH HEADERS FROM 'file:///XXX.csv' AS row
MATCH (a1:Person), (a2:Person)
WHERE a1.uuid = row.uuid1 AND a2.uuid = row.uuid2
WITH [a1,a2] as nodes
// apoc.refactor.mergeNodes part 

or even better, to avoid "cartesian product" issues (due to MATCH (a1:Person), (a2:Person), in fact, in neo4j browser you can see the query underlined in yellow, because is a warning),
you could execute:

LOAD CSV WITH HEADERS FROM 'file:///XXX.csv' AS row
MATCH (a1:Person)
WHERE a1.uuid = row.uuid1
WITH a1
MATCH (a2:Person)
WHERE a2.uuid = row.uuid2
// apoc.refactor.mergeNodes part