MERGE command is a combination of CREATE command and MATCH command.
Neo4j CQL MERGE command searches for a given sample in the graph. If it exists, then it returns the effects.
If it does NOT exist inside the graph, then it creates a new node/dating and returns the effects.
In this chapter you are going to learn how to −
- Merge a node with label
- Merge a node with houses
- OnCreate and OnMatch
- Merge a relationship
Syntax
Following is the syntax for the MERGE command.
MERGE (node: label {properties . . . . . . . })
Before intending to the examples on this segment, create nodes inside the database with labels Dhawan and Ind. Create a dating of type “BATSMAN_OF” from Dhawan to Ind as shown underneath.
CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"})
CREATE (Ind:Country {name: "India"})
CREATE (Dhawan)-[r:BATSMAN_OF]->(Ind)
Merging a Node with a Label
You can merge a node within the database based totally on the label the use of the MERGE clause. If you try to merge a node primarily based at the label, then Neo4j verifies whether or not there exists any node with the given label. If not, the contemporary node will be created.
Syntax
Following is the syntax to merge a node primarily based on a label.
MERGE (node:label) RETURN node
Example 1
Following is a sample Cypher Query which merges a node into Neo4j (based on label). When you execute this query, Neo4j verifies whether or not there is any node with the label player. If now not, it creates a node named “Jadeja” and returns it.
If, there exists any node with the given label, Neo4j returns all of them.
MERGE (Jadeja:player) RETURN Jadeja
To execute the above question, perform the subsequent steps −
Step 1 − Open the Neo4j computer App and begin the Neo4j Server. Open the integrated browser app of Neo4j the use of the URL http://localhost:7474/ as proven within the following screenshot.
Step 2 − Copy and paste the favored query inside the dollar spark off and press the play button (to execute the query) highlighted inside the following screenshot.
Result
On executing, you'll get the following result. Since you have already created a node named “Dhawan” with the label “participant” inside the database, Neo4j returns it as proven inside the following screenshot.
Example 2
Now, try to merge a node named “CT2013” with a label named Tournament. Since there aren't any nodes with this label, Neo4j creates a node with the given name and returns it.
MERGE (CT2013:Tournament{name: "ICC Champions Trophy 2013"})
RETURN CT2013, labels(CT2013)
To execute the above question, carry out the following steps −
Step 1 − Open the Neo4j computing device App and Start the Neo4j Server. Open the built-in browser app of Neo4j the usage of the URL http://localhost:7474/ as proven inside the following screenshot.
Step 2 − Copy and paste the favored question in the greenback set off and press the play button (to execute the query) highlighted within the following screenshot.
Result
On executing, you may get the subsequent result. As mentioned, seeing that there's no node with the given label (Tournament). Neo4j creates and returns the specified node as proven in the following screenshot.
Merging a Node with Properties
You can also merge a node with a hard and fast of properties. If you do so, Neo4j searches for an equal match for the specified node, inclusive of the properties. If it doesn’t locate any, it creates one.
Syntax
Following is the syntax to merge a node using residences.
MERGE (node:label {key1:value, key2:value, key3:value . . . . . . . . })
Example
Following is a sample Cypher Query to merge a node using houses. This query tries to merge the node named “jadeja” the use of homes and label. Since there's no such node with the exact label and homes, Neo4j creates one.
MERGE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})
RETURN Jadeja
To execute the above question, carry out the following steps −
Step 1 − Open the Neo4j computer App and Start the Neo4j Server. Open the integrated browser app of Neo4j the usage of the URL http://localhost:7474/ as proven in the following screenshot.
Step 2 − Copy and paste the desired question within the greenback set off and press the play button (to execute the query) highlighted inside the following screenshot.
Result
On executing, you may get the subsequent end result. As mentioned, due to the fact there aren't any nodes with the specified label and properties, it creates one, as proven inside the following screenshot.
OnCreate and OnMatch
Whenever, we execute a merge question, a node is both matched or created. Using on create and on in shape, you may set homes for indicating whether the node is created or matched.
Syntax
Following is the syntax of OnCreate and OnMatch clauses.
MERGE (node:label {properties . . . . . . . . . . .})
ON CREATE SET property.isCreated ="true"
ON MATCH SET property.isFound ="true"
Example
Following is a pattern Cypher Query which demonstrates the use of OnCreate and OnMatch clauses in Neo4j. If the required node already exists inside the database, then the node might be matched and the belongings with key-value pair isFound = "real" will be created within the node.
If the required node doesn’t exist inside the database, then the node can be created, and inside it a belongings with a key-fee pair isCreated ="proper" can be created.
MERGE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})
ON CREATE SET Jadeja.isCreated = "true"
ON MATCH SET Jadeja.isFound = "true"
RETURN Jadeja
To execute the above query, perform the subsequent steps −
Step 1 − Open the Neo4j laptop App and begin the Neo4j Server. Open the integrated browser app of Neo4j using the URL http://localhost:7474/ as shown within the following screenshot.
Step 2 − Copy and paste the preferred query within the dollar activate and press the play button (to execute the question) highlighted inside the following screenshot.
Result
On executing, you will get the subsequent end result. As mentioned, on the grounds that there may be no node with the desired information, Neo4j created it at the side of the belongings isFound as shown within the following screenshot.
Merge a Relationship
Just like nodes, you could also merge the relationships the use of the MERGE clause.
Example
Following is a pattern Cypher Query which merges a relationship the usage of the MATCH clause in Neo4j. This query attempts to merge a courting named WINNERS_OF among the nodes “ind” (label: Country & call: India) and ICC13 (label: Tournament & call: ICC Champions Trophy 2013).
Since such relation doesn’t exist, Neo4j creates one.
MATCH (a:Country), (b:Tournament)
WHERE a.name = "India" AND b.name = "ICC Champions Trophy 2013"
MERGE (a)-[r:WINNERS_OF]->(b)
RETURN a, b
To execute the above question, carry out the subsequent steps −
Step 1 − Open the Neo4j laptop App and begin the Neo4j Server. Open the built-in browser app of Neo4j the usage of the URL http://localhost:7474/ as proven inside the following screenshot.
Step 2 − Copy and paste the preferred question within the greenback set off and press the play button (to execute the question) highlighted in the following screenshot.
Result
On executing, you'll get the subsequent end result. Since the specified relation doesn’t exist inside the database, Neo4j creates one as proven within the following screenshot.
In the same manner, you can merge a couple of relationships and undirected relationships too.