折叠节点

限定名称 类型

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