[英]cypher: merge two nodes with same attributes and different relationships
lets say, I have two nodes with the same label and same attribute values:可以说,我有两个具有相同标签和相同属性值的节点:
Create (n:A {foo: 'bar'});
Create (m:A {foo: 'bar'});
I have also some other nodes:我还有一些其他节点:
Create(o:B {test: 'test'});
Create(p:C {other: 'other'});
And I have relationships from the first nodes to the other nodes:我有从第一个节点到其他节点的关系:
Match (n:A {foo: 'bar'}), (o:B {test: 'test'}) MERGE (n)-[:r]-(o);
Match (m:A {foo: 'bar'}), (p:C {other: 'other'}) MERGE (m)-[:s]-(p);
So I get a graph shown in the picture:所以我得到了如图所示的图表:
Now I want to combine the two nodes of type A to one node and keep both relationships.现在我想将 A 类型的两个节点合并为一个节点并保持两种关系。 So I want to get a graph similar as shown in the picture:
所以我想得到一个如图所示的图形:
Is there a cypher query to do this?是否有密码查询来执行此操作? Especially to do this with all nodes of one type which have the same attribute properties?
特别是要对具有相同属性的一种类型的所有节点执行此操作?
We have a procedure in APOC to do that : apoc.refactor.mergeNodes
我们在APOC 中有一个程序可以做到这一点:
apoc.refactor.mergeNodes
This is the link to the documentation : https://neo4j-contrib.github.io/neo4j-apoc-procedures/#merge-nodes这是文档的链接: https : //neo4j-contrib.github.io/neo4j-apoc-procedures/#merge-nodes
ANd the solution for your example :和您的示例的解决方案:
MATCH (n:A {foo: 'bar'})
WITH collect(n) AS nodes
CALL apoc.refactor.mergeNodes(nodes, {properties:"override", mergeRels:true}) yield node
RETURN node
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.