eighteyes
eighteyes

Reputation: 1314

Merge existing records in neo4j, remove duplicates, keep relationships

I've imported my millions of records using CREATE for performance reasons, now I want to MERGE the records together, and keep all the relationships intact.

Any ideas?

EDIT:

MATCH (c1:company), (c2:company) 
WITH c1, c2 
WHERE c1.name = c2.name 
SET c1=c2

Is the type of thing I'm looking for.

Upvotes: 2

Views: 1952

Answers (2)

Jim Biard
Jim Biard

Reputation: 2272

It doesn't work that way. There is no way to move relationships around, and no way to coalesce existing nodes. You should use MERGE from the beginning, along with constraints and indexes to aid performance.

Upvotes: 1

Michael Hunger
Michael Hunger

Reputation: 41706

If you want to merge nodes in cypher you can do something like this:

MATCH (c:Company)
WITH c.name as name, collect(c) as companies, count(*) as cnt
WHERE cnt > 1
WITH head(companies) as first, tail(companies) as rest
LIMIT 1000
UNWIND rest AS to_delete
MATCH (to_delete)<-[r:WORKS_AT]-(e:Employee)
MERGE (first)<-[:WORKS_AT]-(e)
DELETE r
DELETE to_delete
RETURN count(*);

see: http://www.neo4j.org/graphgist?dropbox-14493611%2Fmerge_nodes.adoc

Upvotes: 5

Related Questions