apoc.merge.relationshipWithStats

Details

Syntax

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

Description

Merges the given RELATIONSHIP values with the given dynamic types/properties. Provides queryStatistics in the result.

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

stats

MAP

The returned query statistics.

rel

RELATIONSHIP

The updated relationship.

Example

Given this dataset:

CREATE (:Person {name: 'Alice'}), (:Person {name: 'Bob'})

The following query merges a KNOWS relationship between two existing nodes and returns the relationship alongside query statistics:

MATCH (a:Person {name: 'Alice'}), (b:Person {name: 'Bob'})
CALL
  apoc.merge.relationshipWithStats(
    a,
    'KNOWS',
    {id: 1},
    {since: 2019},
    b,
    {}
  )
  YIELD rel, stats
RETURN
  type(rel) AS type,
  rel.id AS id,
  rel.since AS since,
  stats.relationshipsCreated AS relationshipsCreated,
  stats.propertiesSet AS propertiesSet
Results
type id since relationshipsCreated propertiesSet

"KNOWS"

1

2019

1

2

relationshipsCreated is 1 because no matching KNOWS relationship existed. propertiesSet is 2, covering id and since. Calling the procedure again with the same identProps, the existing relationship is matched instead: relationshipsCreated becomes 0 and only the onMatchProps changes are reflected in propertiesSet.