I made a test file with the following circuit:
CREATE (n1:TestX {name:'A', val1:1}) CREATE (n2:TestX {name:'B', val2:2}) CREATE (n3:TestX {name:'B', val3:3}) CREATE (n4:TestX {name:'B', val4:4}) CREATE (n5:TestX {name:'C', val5:5}) MATCH (n6:TestX {name:'A', val1:1}) MATCH (m7:TestX {name:'B', val2:2}) CREATE (n6)-[:TEST]->(m7) MATCH (n8:TestX {name:'C', val5:5}) MATCH (m10:TestX {name:'B', val3:3}) CREATE (n8)<-[:TEST]-(m10)
Which leads to the following result:

Where the nodes of B are really the same nodes. And here is my solution:
//copy all properties MATCH (n:TestX), (m:TestX) WHERE n.name = m.name AND ID(n)<ID(m) WITH n, m SET n += m; //copy all outgoing relations MATCH (n:TestX), (m:TestX)-[r:TEST]->(endnode) WHERE n.name = m.name AND ID(n)<ID(m) WITH n, collect(endnode) as endnodes FOREACH (x in endnodes | CREATE (n)-[:TEST]->(x)); //copy all incoming relations MATCH (n:TestX), (m:TestX)<-[r:TEST]-(endnode) WHERE n.name = m.name AND ID(n)<ID(m) WITH n, collect(endnode) as endnodes FOREACH (x in endnodes | CREATE (n)<-[:TEST]-(x)); //delete duplicates MATCH (n:TestX), (m:TestX) WHERE n.name = m.name AND ID(n)<ID(m) detach delete m;
The result obtained is as follows:

It should be noted that you need to know the type of various relationships.
All properties are copied from nodes with "higher" identifiers to nodes with "lower" identifiers.
Ke
source share