MERGE

The MERGE clause ensures that a specified pattern exists in the graph — either by matching existing data or by creating it.

MERGE either matches an existing vertex or creates new data. It combines the behavior of MATCH and CREATE.

Note!
Restrictions on MERGE in YMatrix AGE:

  1. ON MATCH SET and ON CREATE SET syntax is not supported.

    MERGE (n:node {name: 'Lisa'}) ON MATCH SET n.age = 23  
    -- ERROR: syntax error at or near "ON"  
  2. Combining MERGE with other write operations (CREATE, SET) via WITH raises an error.

  3. Distributed transaction errors occur when MERGE is chained with CREATE or SET using WITH to pass bound variables:

    
    -- Error  
    CREATE (n) WITH n AS a MERGE (a)-[:e]->()  
    -- ERROR: AssignTransactionId() called by Segment Reader process  

-- Error
CREATE (n {i : 1}) SET n.i = 2 MERGE ({i: 2})
-- ERROR: AssignTransactionId() called by Segment Reader process



**Recommendation:** Split `MERGE` and other write operations into separate Cypher statements.

For example, you may specify that the graph must contain a user vertex with a particular name. If no vertex with that name exists, a new one is created and its name property is set. When MERGE is applied to a full pattern, the behavior is all-or-nothing: either the entire pattern matches, or the entire pattern is created. MERGE does not partially reuse existing patterns. To achieve partial matching, decompose the pattern into multiple MERGE clauses.

Like MATCH, MERGE can match multiple occurrences of a pattern. If multiple matches exist, all are passed to subsequent query stages.

Data Preparation

SELECT * FROM cypher('graph_name', $$
CREATE (A:Person {name: "Charlie Sheen", bornIn: "New York"}),
    (B:Person {name: "Michael Douglas", bornIn: "New Jersey"}),
    (C:Person {name: "Rob Reiner", bornIn: "New York"}),
    (D:Person {name: "Oliver Stone", bornIn: "New York"}),
    (E:Person {name: "Martin Sheen", bornIn: "Ohio"})
$$) AS (result agtype);

Merging Vertices

Merging a Labeled Vertex

To merge a vertex with a label, specify a pattern containing a single labeled vertex.

Query

SELECT * FROM cypher('graph_name', $$
MERGE (v:Critic)
RETURN v
$$) AS (v agtype);

If a vertex labeled Critic exists, it is returned. Otherwise, a new Critic vertex is created and returned.

v
{id: 0; label: 'Critic'; properties: {}}::vertex
1 row returned

Merging a Single Vertex with Properties

Merge a vertex where not all properties match an existing vertex.

Query

SELECT * FROM cypher('graph_name', $$
MERGE (charlie {name: 'Charlie Sheen', age: 10})
RETURN charlie
$$) AS (v agtype);

If a vertex with all specified properties exists, it is returned. Otherwise, a new vertex named 'Charlie Sheen' is created and returned.

v
{id: 0; label: 'Actor'; properties: {name: 'Charlie Sheen', age: 10}}::vertex
1 row returned

Merging a Single Vertex with Both Label and Properties

Merge a vertex constrained by both label and properties that match an existing vertex.

Query

SELECT * FROM cypher('graph_name', $$
MERGE (michael:Person {name: 'Michael Douglas'})
RETURN michael.name, michael.bornIn
$$) AS (Name agtype, BornIn agtype);

'Michael Douglas' matches the existing Person vertex, and the query returns its name and bornIn properties.

Name BornIn
"Michael Douglas" "New Jersey"
1 row returned