简体   繁体   English

cypher:合并具有相同属性和不同关系的两个节点

[英]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.

相关问题 Cypher:查询以选择由具有相同属性的相同关系的两个实例连接的两个节点? - Cypher: Query to select two nodes connected by two instances of the same relation with different attributes? 如何合并两个具有不同属性的节点合并为密码中的一个节点? - how to combine two nodes with different properties merge as one node in cypher? 相同类型的多个关系,但在相同的两个节点之间具有不同的属性 - Multiple relationships of the same type but with different properties between the same two nodes 如何使用Cypher获得两个节点之间相同关系的数量 - How do i get number of same relationships between two nodes with Cypher 使用Cypher在Neo4j中的两个节点之间创建相同类型的多个关系 - Creating multiple relationships of the same type between two nodes in Neo4j using Cypher 克隆节点和与 Cypher 的关系 - Clone nodes and relationships with Cypher 在 Neo4j Cypher 中创建同类节点的关系 - Creating relationships of nodes of the same kind in Neo4j Cypher 如何使用Cypher在neo4j中“组合”两个节点和关系 - How to “combine” two nodes and relationships in neo4j using Cypher 简化cypher,匹配所有节点和两种节点类型之间的关系 - Simplifying cypher, match all nodes and relationships between two node types Neo4j cypher查询,计算节点和双向关系 - Neo4j cypher queries, counting nodes and two way relationships
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM