0
votes

My question is regarding how to merge multiple duplicate nodes.

Each node is, as a duplicate, connected to other nodes with a MENTIONED edge.

What I would like to do is to merge those duplicate nodes based on two properties being identical (the properties are entity_type and name so where entity_type=entity_type and name=name then merge those two nodes) and to also be able to preserve the relationships those nodes had pre-merge to other nodes on the new node. So the new merged node might have 2+ relationships to other nodes that the nodes it was merged from had.

Does anyone have any advice on how to structure this query? Thank you very much.

1

1 Answers

0
votes

You should use APOC library to do this. APOC has a procedure apoc.refactor.mergeNodes which can merge multiple duplicate nodes into one node.

All relationships are merged onto that node too.

You can refer the following query to structure yours:

MATCH (p:Entity)
WITH p.name AS name, p.entity_type AS type, collect(p) as nodes
CALL apoc.refactor.mergeNodes(nodes, {properties: {`.*`: 'discard'}}) YIELD node
RETURN count(node) AS new_node_count