Collapse Nodes

Qualified Name Type Release

apoc.nodes.collapse

Procedure

APOC Core

Config:

On apoc.nodes.collapse with config properties you can choose from 3 different behavior:

  • "properties": "overwrite" : if there is the same property in more node, in the new one will have the last relationship’s/node’s property value

  • "properties": "discard" : if there is the same property in more node, the new one will have the first relationship’s/node’s property value

  • "properties": "combine" : if there is the same property in more node, the new one a value’s array with all relationship’s/node’s values

If properties parameter isn’t set relationships properties are discard.

  • "mergeRelsVirtual: true/false" : give the possibility to merge relationships with same type and direction. (DEFAULT true)

  • "selfRel: true/false" : give the possibility to create the self relationship. (DEFAULT false)

  • "countMerge: true/false" : give the possibility count all the Nodes/Relationships merged. (DEFAULT true)

  • "collapsedLabel: true/false" : give the possibility to add the label :Collapsed to the virtualNode. (DEFAULT false)

Nodes collapse example

With this dataset we have:

apoc.nodes.collapse 1

If we want to collapse the people living in the city to a single node, we pass them to the procedure.

MATCH (p:Person)-[:LIVES_IN]->(c:City)
WITH c, collect(p) as subgraph
CALL apoc.nodes.collapse(subgraph,{properties:'combine'}) yield from, rel, to
return from, rel, to

And get this result:

apoc.nodes.collapse 2

With this dataset we have:

apoc.nodes.collapse 3

If we also want to collapse them onto the city itself, we add the city node first to the collection.

MATCH (p:Person)-[:LIVES_IN]->(c:City)
WITH c, c + collect(p) as subgraph
CALL apoc.nodes.collapse(subgraph) yield from, rel, to
return from, rel, to

And get this result:

apoc.nodes.collapse 4