Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neo4j merge 2 or multiple duplicate nodes

Tags:

neo4j

cypher

I am feeding my neo4j db manually using cypher, so prone to error like creating duplicate nodes:

The duplicate nodes will have each relationships to other nodes. Is there a built-in function to merge these nodes? Or should I do it manually?

Sounds possible, but complicated with cypher script:

    1. Get the relationships of each duplicate node
    1. Recreate them (with their properties) with the correct node (given node id)
    1. Remove relationships to the duplicate nodes
    1. and finally remove the duplicate nodes.
like image 299
AJN Avatar asked Apr 29 '14 23:04

AJN


People also ask

How do I merge two nodes in Neo4j?

You can merge a node in the database based on the label using the MERGE clause. If you try to merge a node based on the label, then Neo4j verifies whether there exists any node with the given label. If not, the current node will be created.

How does merge work in Neo4j?

What is MERGE, and how does it work? The MERGE clause ensures that a pattern exists in the graph. Either the entire pattern already exists, or the entire pattern needs to be created. In this way, it's helpful to think of MERGE as attempting a MATCH on the pattern, and if no match is found, a CREATE of the pattern.

Can u relabel node in Neo4j?

You can change the nodes associated with a label but you can't change the type of a relationship. Conceptually, if you take your chicken out of one coop and put it in another you haven't altered the substance of the chicken.

How do I add multiple nodes in Neo4j?

The create clause of Neo4j CQL is also used to create multiple nodes at the same time. To do so, you need to pass the names of the nodes to be created, separated by a comma.


2 Answers

To avoid this situation in the future, please look at the MERGE keyword in Cypher. Unfortunately, as far as I know, there is nothing in Cypher (yet) like:

MATCH (n:MyNode),(m:MyNode)
WHERE ID(n) <> ID(m) AND
PROPS(n) IN PROPS(m) AND PROPS(m) IN PROPS(n)
(...) DELETE (...)

The fictional function PROPS of the third line is not part of Cypher language and User-Defined functions have not made it yet into Neo4j.

If you're not working with production instances, the easiest is probably to back up your data folder and try to start the insertion over (with MERGE).

Otherwise, you can also try writing a traversal to collect the duplicates and delete them in batch (here is an example with the REST API).

like image 171
fbiville Avatar answered Oct 24 '22 08:10

fbiville


Try this:

MATCH (n:MyNode),(m:MyNode),(o:OtherNode {id:123})
WHERE n <> m
MATCH (m)-[r:FOO]->()
CREATE (n)-[r2:FOO]->(o)
SET r2 = r
DELETE r,m
like image 40
Michael Hunger Avatar answered Oct 24 '22 08:10

Michael Hunger