apoc.refactor.mergeRelationshipsProcedure
Syntax |
|
||
Description |
Merges the given |
||
Input arguments |
Name |
Type |
Description |
|
|
The relationships to be merged onto the first relationship. |
|
|
|
|
|
Return arguments |
Name |
Type |
Description |
|
|
The merged relationship. |
|
Example
Given this dataset:
CREATE (a:Person {name: 'Alice'})-[:KNOWS {since: 2019}]->(b:Person {name: 'Bob'})
CREATE (a)-[:MET {since: 2021, place: 'London'}]->(b)
The following query merges both relationships between Alice and Bob onto the first one, with later property values overwriting earlier ones:
MATCH (a:Person {name: 'Alice'})-[r1:KNOWS]->(b:Person {name: 'Bob'})
MATCH (a)-[r2:MET]->(b)
CALL apoc.refactor.mergeRelationships([r1, r2], {properties: 'overwrite'})
YIELD rel
RETURN type(rel) AS type, rel.since AS since, rel.place AS place
| type | since | place |
|---|---|---|
"KNOWS" |
2021 |
"London" |
The first relationship in the list (KNOWS) is kept and the rest are deleted.
since is overwritten to 2021 by the later MET relationship; place is added from MET since KNOWS had no such property.
All relationships in the list must share the same start and end nodes, or the procedure will throw an error.
Use properties: 'discard' to keep the first value for each conflicting property, or properties: 'combine' to merge conflicting values
into arrays.