Merge all nodes with the same property name

(Joe) #1

Hi, I've a problem that I do not know how to code in cypher. I have duplicate nodes with the same property name, (n.name) and they have their own relationships. I wanted to match these nodes, merges the properties and relationships of the 2nd through last nodes onto the first node, and deletes the 2nd through last nodes. I have the code below and it works.

MATCH (n:name)
WHERE n.name = "john"
WITH COLLECT(n) AS ns
CALL apoc.refactor.mergeNodes(ns) YIELD node
RETURN node;

However, I do not want to input the name manually. Is it possible to do a for loop all the nodes that have the same property name and do the above code? like john, jack, jane ...

0 Likes

(Bratanic Tomaz) #2

to find nodes with the same property value

MATCH (n1:name),(n2:name)
WHERE n1.name = n2.name and id(n1) < id(n2)
WITH [n1,n2] as ns
CALL apoc.refactor.mergeNodes(ns) YIELD node
RETURN node
0 Likes

(Joe) #3

id(n1) < id(n2)
don't quite get this and got syntax error
does the id refer to the internal id that neo4j created? i do not have id as property in the node

0 Likes

(Bratanic Tomaz) #4

Yes, this is internal id of Neo4j. I fixed the syntax error

0 Likes

(Joe) #5

works! thanks alot!!!!!

0 Likes

(IM) #6

Hello, I have actually been looking for a solution like that as well. For me it however leads to a Cartesian product? Is there a way to avoid that?

0 Likes

(Andrew Bowman) #7

Yes, it will look similar to the query in the first post, except we'll collect with respect to name (for each row/name, we'll get the collection of nodes with that name) and filter to only rows where there are multiple nodes for that name:

MATCH (n:name)
WITH n.name as name, COLLECT(n) AS ns
WHERE size(ns) > 1
CALL apoc.refactor.mergeNodes(ns) YIELD node
RETURN node;
0 Likes

(IM) #8

Brilliant, thanks a lot!!!

0 Likes

(Joe) #9

Hi, I've an error if the property of my nodes are uniquely indexed. Are there any work around with it?

New data does not satisfy CONSTRAINT ON ( label[0]:label[0] ) ASSERT label[0].property[0] IS UNIQUE.
0 Likes

(Joe) #10

Nvm solved it using this

CALL apoc.refactor.mergeNodes(nodes,{properties:"combine", mergeRels:true}) yield node
0 Likes