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:
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.
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.
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.
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With