Neo4j 简明教程

Neo4j - Merge Command

MERGE 命令是 CREATE 命令和 MATCH 命令的组合。

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

Neo4j CQL MERGE 命令在图中搜索给定的模式。如果存在,则它返回结果。

Neo4j CQL MERGE command searches for a given pattern in the graph. If it exists, then it returns the results.

如果在图中不存在,则它创建一个新节点/关系并返回结果。

If it does NOT exist in the graph, then it creates a new node/relationship and returns the results.

在本章中,你将学习如何 −

In this chapter you are going to learn how to −

  1. Merge a node with label

  2. Merge a node with properties

  3. OnCreate and OnMatch

  4. Merge a relationship

Syntax

下面是 MERGE 命令的语法。

Following is the syntax for the MERGE command.

MERGE (node: label {properties . . . . . . . })

在继续本节中的示例之前,请在数据库中创建两个带有标签 Dhawan 和 Ind 的节点。创建从 Dhawan 到 Ind 的类型为 “BATSMAN_OF” 的关系,如下所示。

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.

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

可以使用 MERGE 子句根据标签合并数据库中的节点。如果尝试根据标签合并节点,则 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.

Syntax

以下是根据标签合并节点的语法。

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

MERGE (node:label) RETURN node

Example 1

下面是一个 Cypher 查询示例,它将一个节点合并到 Neo4j 中(根据标签)。执行此查询时,Neo4j 会验证是否存在任何标签为 player 的节点。如果没有,它会创建一个名为 “Jadeja” 的节点并返回它。

Following is a sample Cypher Query which merges a node into Neo4j (based on label). When you execute this query, Neo4j verifies whether there is any node with the label player. If not, it creates a node named “Jadeja” and returns it.

如果存在任何带有给定标签的节点,Neo4j 会返回它们全部。

If, there exists any node with the given label, Neo4j returns them all.

MERGE (Jadeja:player) RETURN Jadeja

执行上述查询,执行以下步骤:

To execute the above query, carry out the following steps −

Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。

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 the following screenshot.

browser app

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。

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

merge

Result

执行后,你将获得以下结果。由于你已在数据库中创建了一个标签为 “player” 的名为 “Dhawan” 的节点,所以 Neo4j 会按以下屏幕截图所示返回它。

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

dhawan player

Example 2

现在,尝试合并一个名为 “CT2013” 的节点,其标签名为 Tournament。由于没有带有此标签的节点,Neo4j 会创建一个带有给定名称的节点并返回它。

Now, try to merge a node named “CT2013” with a label named Tournament. Since there are no 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 query, carry out the following steps −

Step 1 - 打开 Neo4j 桌面应用程序并启动 Neo4j 服务器。使用 URL 打开 Neo4j 的内置浏览器应用程序 http://localhost:7474/ ,如下图所示。

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 the following screenshot.

browser app

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。

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

merging node

Result

在执行时,您将获得以下结果。正如所讨论的,由于没有具有给定标签(Tournament)的节点。Neo4j 创建并返回指定节点,如下图所示。

On executing, you will get the following result. As discussed, since there is no node with the given label (Tournament). Neo4j creates and returns the specified node as shown in the following screenshot.

tornament

Merging a Node with Properties

您还可以使用一组属性合并节点。如果您这样做,Neo4j 将搜索与指定节点相等的匹配项,包括属性。如果找不到任何匹配项,则创建一个匹配项。

You can also merge a node with a set of properties. If you do so, Neo4j searches for an equal match for the specified node, including the properties. If it doesn’t find any, it creates one.

Syntax

以下是使用属性合并节点的语法。

Following is the syntax to merge a node using properties.

MERGE (node:label {key1:value, key2:value, key3:value . . . . . . . . })

Example

以下是一个示例 Cypher 查询,用于使用属性合并节点。此查询尝试使用属性和标签合并名为 “jadeja” 的节点。由于没有具有确切标签和属性的此类节点,因此 Neo4j 创建了一个。

Following is a sample Cypher Query to merge a node using properties. This query tries to merge the node named “jadeja” using properties and label. Since there is no such node with the exact label and properties, Neo4j creates one.

MERGE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})
RETURN Jadeja

执行上述查询,执行以下步骤:

To execute the above query, carry out the following steps −

Step 1 - 打开 Neo4j 桌面应用程序并启动 Neo4j 服务器。使用 URL 打开 Neo4j 的内置浏览器应用程序 http://localhost:7474/ ,如下图所示。

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 the following screenshot.

browser app

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。

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

merging properties

Result

在执行时,您将获得以下结果。正如所讨论的,由于没有具有指定标签和属性的节点,因此它创建了一个,如下图所示。

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

property result

OnCreate and OnMatch

每当我们执行合并查询时,一个节点要么匹配,要么创建。使用 on create 和 on match,您可以设置属性来指示节点是创建还是匹配。

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

Syntax

以下是 OnCreateOnMatch 子句的语法。

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

以下是一个示例 Cypher 查询,演示了在 Neo4j 中使用 OnCreateOnMatch 子句。如果指定的节点已存在于数据库中,则将匹配该节点并在该节点中创建具有键值对 isFound = "true" 的属性。

Following 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.

如果指定的节点不存在于数据库中,则将创建该节点,并在其中创建具有键值对 isCreated ="true" 的属性。

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.

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, carry out the following steps −

Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。

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 the following screenshot.

browser app

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。

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

match set

Result

在执行时,您将获得以下结果。正如所讨论的,由于没有具有指定详细信息的节点,因此 Neo4j 创建了它以及属性 isFound ,如下图所示。

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

oncreate result

Merge a Relationship

就像节点一样,您还可以使用 MERGE 子句合并关系。

Just like nodes, you can also merge the relationships using the MERGE clause.

Example

以下是一个示例 Cypher 查询,它使用 Neo4j 中的 MATCH 子句合并关系。此查询尝试合并节点 “ind”(标签:国家和名称:印度)和 ICC13(标签:锦标赛和名称:ICC 冠军奖杯 2013)之间的名为 WINNERS_OF 的关系。

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

由于不存在此类关系,因此 Neo4j 创建了一个。

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 query, carry out the following steps −

Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。

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 the following screenshot.

browser app

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。

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

winners list

Result

在执行时,您将获得以下结果。由于指定的关联不存在于数据库中,因此 Neo4j 会创建一个,如下图所示。

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

relationship merge

同样,您也可以合并多个关系和无方向关系。

In the same way, you can merge multiple relationships and undirected relationships too.