折叠节点

限定名称 类型

apoc.nodes.collapse
apoc.nodes.collapse(nodes LIST<NODE>, config MAP<STRING, ANY>) - 将给定 LIST<NODE> 中的 NODE 值合并在一起。这些 NODE 值随后被组合成一个 NODE,并附带所有先前 NODE 值的标签,以及所有指向它的 RELATIONSHIP 值。

过程

配置

在带有配置属性的 apoc.nodes.collapse 中,您可以选择 3 种不同的行为

  • "properties": "overwrite" : 如果多个节点中存在相同属性,则新节点将拥有最后一个关系/节点的属性值

  • "properties": "discard" : 如果多个节点中存在相同属性,则新节点将拥有第一个关系/节点的属性值

  • "properties": "combine" : 如果多个节点中存在相同属性,则新节点将拥有一个包含所有关系/节点值的数组

如果未设置 properties 参数,则关系属性将 discard

  • "mergeRelsVirtual: true/false" : 允许合并具有相同类型和方向的关系。(默认 true)

  • "selfRel: true/false" : 允许创建自循环关系。(默认 false)

  • "countMerge: true/false" : 允许统计所有合并的节点/关系。(默认 true)

  • "collapsedLabel: true/false" : 允许将标签 :Collapsed 添加到虚拟节点。(默认 false)

节点折叠示例

使用此数据集,我们有

apoc.nodes.collapse 1

如果我们要将居住在城市中的人折叠成一个单一节点,我们会将他们传递给该过程。

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

并获得此结果

apoc.nodes.collapse 2

使用此数据集,我们有

apoc.nodes.collapse 3

如果我们还想将它们折叠到城市本身,我们首先将城市节点添加到集合中。

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

并获得此结果

apoc.nodes.collapse 4