Neo4j Cypher合并节点并持久化

huangapple go评论53阅读模式
英文:

Neo4j Cypher merge nodes and persist

问题

我正在尝试合并数据库中的重复节点,它们具有一些共享属性但不相同。我正在找到它们并正确合并,但数据库没有发生任何更改。

这是我的查询:

MATCH (root:Person { firstName: "X", lastName: "Y" }),(p)
WHERE root.firstName = p.firstName AND root.lastName = p.lastName
AND root.network = "D" AND p.network = "D"
WITH head(collect([root,p])) as nodes
CALL apoc.refactor.mergeNodes(nodes,{
properties:'combine',
mergeRels:true
})
YIELD node
return node

我原本期望它会用新节点替换原始的(2)节点,然而实际上它只返回了该节点,而底层数据没有发生任何更改。

英文:

I'm trying to merge duplicate nodes in my database, they have some shared properties but are not the same. I'm finding them and merging it correctly, but there is no change to the database.

This is my query:

MATCH (root:Person { firstName: "X", lastName: "Y" }),(p)
WHERE root.firstName = p.firstName AND root.lastName = p.lastName
AND root.network = "D" AND p.network = "D"
WITH head(collect([root,p])) as nodes
CALL apoc.refactor.mergeNodes(nodes,{
properties:'combine',
mergeRels:true
})
YIELD node
return node

I was expecting this to replace the original (2) nodes with this new node, however all this does is return the node and there is no change to the underlying data.

答案1

得分: 1

我已经弄清楚了 - 它似乎确实更新了数据库。我遇到的问题是MATCH语句创建了一个笛卡尔连接 - 当我调用HEAD时,它只获取第一个元素,即第一个节点和它本身。通过添加以下行:
AND id(root) <> id(p)(当然,在WHERE语句中)可以防止这种行为,使其按预期运行。

英文:

I figured this out - it does actually appear to update the database. The issue I had was the MATCH statement created a cartesian join - and when I call HEAD it only takes the first element, which is the first node and itself.

By adding the following line:
AND id(root) <> id(p) (in the WHERE statement of course) it prevents this behaviour and works as intended.

答案2

得分: 0

根据mergeNodes()文档,该方法仅合并节点/关系并将结果返回为一个变量 - 它不会将其写回到数据库。

尽管我不知道是否有立即写回结果的替代方法,也许您可以在Neo4j的Discord服务器上发布这个问题。

英文:

According to the documentation of mergeNodes(), this method just merges nodes/relations and returns the result as a variable - it does not write it back to the database.

Though I don't know of an alternative which writes the result back immediately, maybe you can post this question on the Neo4j's Discord server.

huangapple
  • 本文由 发表于 2023年3月1日 10:22:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定