apoc.refactor.mergeRelationships

Details

Syntax

apoc.refactor.mergeRelationships(rels [, config ]) :: (rel)

Description

Merges the given LIST<RELATIONSHIP> onto the first RELATIONSHIP in the LIST<RELATIONSHIP>.

Input arguments

Name

Type

Description

rels

LIST<RELATIONSHIP>

The relationships to be merged onto the first relationship.

config

MAP

{ mergeRels :: BOOLEAN, selfRef :: BOOLEAN, produceSelfRef = true :: BOOLEAN, preserveExistingSelfRels = true :: BOOLEAN, countMerge = true :: BOOLEAN, collapsedLabel :: BOOLEAN, singleElementAsArray = false :: BOOLEAN, avoidDuplicates = false :: BOOLEAN, relationshipSelectionStrategy = "incoming" :: ["incoming", "outgoing", "merge"] properties :: ["overwrite", "discard", "combine"] }. The default is: {}.

Return arguments

Name

Type

Description

rel

RELATIONSHIP

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