折叠节点
| 限定名称 | 类型 |
|---|---|
apoc.nodes.collapse |
|
配置
使用 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)
节点折叠示例
使用此数据集,我们有
如果我们想将居住在同一城市的人员折叠为一个单一节点,我们将他们传递给此过程。
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
并获得以下结果
使用此数据集,我们有
如果我们也想将他们折叠到城市节点本身,我们需要先将城市节点添加到集合中。
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
并获得以下结果