Neo4j Merge Command - Neo4j

How to merge command in Neo4j?

MERGE command in Neo4j is a combination of CREATE command and MATCH command.

Neo4j CQL MERGE command searches for a given pattern in the graph. If it exists, then it will return the results. Else, it will create a new node/relationship and returns the results.

In this chapter we will explain how to

  • Merge a node with label
  • Merge a node with properties
  • OnCreate and OnMatch
  • Merge a relationship

Syntax

Below is the syntax for the MERGE command.

Before proceeding to the examples in this section, create two nodes in the database with labels Dhawan and Ind. Create a relationship of type “BATSMAN_OF” from Dhawan to Ind as shown below.

Merging a Node with a Label

A node in the database can be merged based on the label using the MERGE clause. If you are trying to merge a node based on the label, then Neo4j verifies if there exists any node with the given label. If not, the current node will be created.

Syntax

Below is the syntax to merge a node based on a label.

Example 1

Below is a sample Cypher Query which merges a node into Neo4j (based on label). When this query is executed, Neo4j will verify if there is any node with the label player. If not, it will create a node named “Jadeja” and returns it.

If any node already exists with the given label, Neo4j will return them all.

To execute the above query, carry out below steps

Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in below screenshot.

browser_app

Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in below screenshot.

merge

Result

On executing, you will get below result. As you have already created a node named “Dhawan” with the label “player” in the database, Neo4j will return it as shown in below screenshot.

dhawan_player

Example 2

Now, try to merge a node named “CT2013” with a label named Tournament. As there are no nodes with this label, Neo4j will create a node with the given name and returns it.

To execute the above query, carry out below steps

Step 1 − Open the Neo4j desktop App and Start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in below screenshot.

browser app

Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in below screenshot.

merging_node

Result

Up on executing, you will get below result. As discussed, as there is no node with the given label (Tournament), Neo4j will create and return the specified node as shown in below screenshot.

tornament

Merging a Node with Properties

A node can be merged with a set of properties. If you do so, Neo4j will search for an equal match for the specified node, including the properties. If it doesn’t find any such node it will create one.

Syntax

Below is the syntax to merge a node using properties.

Example

Below is a sample Cypher Query to merge a node using properties. This query will try to merge the node named “jadeja” using properties and label. As there is no such node with the exact label and properties, Neo4j will create one.

To execute the above query, carry out below steps

Step 1 − Open the Neo4j desktop App and Start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in below screenshot.

browser app

Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in below screenshot.

merging_properties

Result

On executing, you will get below result. As discussed, as there are no nodes with the specified label and properties, it will create one as shown in below screenshot.

property_result

OnCreate and OnMatch

Whenever, a merge query is executed, a node will be either matched or created. Using on create and on match, properties can be set for indicating whether the node is created or matched.

Syntax

Below is the syntax of OnCreate and OnMatch clauses.

Example

Below is a sample Cypher Query which demonstrates the usage of OnCreate and OnMatch clauses in Neo4j. If the specified node already exists in the database, then the node will be matched and the property with key-value pair isFound = "true" will be created in the node.

If the specified node doesn’t exist in the database, then the node will be created, and within it a property with a key-value pair isCreated ="true" will be created.

To execute the above query, carry out below steps

Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in below screenshot.

browser app

Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in below screenshot.

match_set

Result

On executing, you will get below result. As discussed, as there is no node with specified details, Neo4j will create it along with the property isFound as shown in below screenshot.

oncreate_result

Merge a Relationship

Just like nodes, relationships can also be merged using the MERGE clause.

Example

Below is a sample Cypher Query which merges a relationship using the MATCH clause in Neo4j. This query will try to merge a relationship named WINNERS_OF between the nodes “ind” (label: Country & name: India) and ICC13 (label: Tournament & name: ICC Champions Trophy 2013).

As such relation doesn’t exist, Neo4j creates one.

To execute the above query, carry out below steps

Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in below screenshot.

browser app

Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in below screenshot.

winners_list

Result

On executing, you will get below result. As the specified relation doesn’t exist in the database, Neo4j creates one as shown in below screenshot.

relationship_merge

In a similar way, multiple relationships can be merged and undirected relationships too.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Neo4j Topics