apoc.merge.relationship

Details

Syntax

apoc.merge.relationship(startNode, relType, identProps, onCreateProps, endNode [, onMatchProps ]) :: (rel)

Description

Merges the given RELATIONSHIP values with the given dynamic types/properties.

Input arguments

Name

Type

Description

startNode

NODE

The start node of the relationship.

relType

STRING

The type of the relationship.

identProps

MAP

Properties on the relationship that are always merged.

onCreateProps

MAP

Properties that are merged when a relationship is created.

endNode

NODE

The end node of the relationship.

onMatchProps

MAP

Properties that are merged when a relationship is matched. The default is: {}.

Return arguments

Name

Type

Description

rel

RELATIONSHIP

The updated relationship.

Usage Examples

The examples in this section are based on the following graph:

CREATE (p:Person {name: "Tom Hanks"})
CREATE (m:Movie {title:"You've Got Mail"});

This procedure provides a more flexible way of merging relationships than Cypher’s MERGE clause.

The example below shows equivalent ways of merging an ACTED_IN relationship between the Tom Hanks and You’ve Got Mail nodes:

apoc.merge.relationship
MATCH (p:Person {name: "Tom Hanks"})
MATCH (m:Movie {title:"You've Got Mail"})
CALL apoc.merge.relationship(p, "ACTED_IN",
  {roles:['Joe Fox']},
  {created: datetime()},
  m,
  {lastSeen: datetime()}
)
YIELD rel
RETURN rel;
MERGE clause
MATCH (p:Person {name: "Tom Hanks"})
MATCH (m:Movie {title:"You've Got Mail"})
MERGE (p)-[rel:ACTED_IN {roles:['Joe Fox']}]->(m)
ON CREATE SET rel.created = datetime()
ON MATCH SET rel.lastSeen = datetime()
RETURN rel;

If we run these queries a few times, we’ll see output as shown below:

Results
rel

[:ACTED_IN {lastSeen: 2020-11-03T11:02:00.261Z, created: 2020-11-03T11:00:56.849Z, roles: ["Joe Fox"]}]

But this procedure is mostly useful for merging relationships that have a dynamic relationship type or dynamic properties. For example, we might want to merge a relationship with a relationship type or properties passed in as parameters.

The following creates relationshipType and properties parameters:

:param relType =>  ("ACTED_IN");
:param properties => ({roles: ["Joe Fox"]});

The following merges a relationship with a relationship type and properties based on the previously defined parameters:

MATCH (p:Person {name: "Tom Hanks"})
MATCH (m:Movie {title:"You've Got Mail"})
CALL apoc.merge.relationship(p, $relType, $properties, {}, m, {})
YIELD rel
RETURN rel;
Results
rel

[:ACTED_IN {roles: ["Joe Fox"]}]