合并具有相同关系的多个节点
merge multiple nodes with the same relationship
任何人都可以帮助我
MATCH(n:Customer)-[r:ORDERS]->(o:Order)<-[r1:ORDERS]-(n1:Customer)
WITH COLLECT(n)+COLLECT(n1) as nodes
CALL apoc.refactor.mergeNodes(nodes,{mergeRels:true}) YIELD node
RETURN node
我实际上想合并 n 和 n1,因为假设 n 有“姓名”属性,n2 有“电子邮件”属性。我试过上面的代码,它说:“Neo.ClientError.Schema.ConstraintValidationFailed”,解释是:“不能删除节点<1397>,因为它仍然有关系。要删除这个节点,你必须先删除它的关系。”
我找到了解决方案:
//SOLUTION
MATCH(n:Customer)-[r:ORDERS]->(o:Order)<-[r1:ORDERS]-(n1:Customer)
WITH o.order_id as i, HEAD(COLLECT([n,n1])) AS nodes
CALL apoc.refactor.mergeNodes(nodes, {mergeRels:true})
YIELD node
RETURN node
它适用于所有称为 n 和 n1 的节点
不要使用 collect(n) + collect(n1),而是使用 head(collect[n, n1]))。此外,您应该在参数中包含组合属性。
OLD:
MATCH(n:Customer)-[r:ORDERS]->(o:Order)<-[r1:ORDERS]-(n1:Customer)
WITH COLLECT(n)+COLLECT(n1) as nodes
CALL apoc.refactor.mergeNodes(nodes,{mergeRels:true}) YIELD node
RETURN node
New:
MATCH(n:Customer)-[r:ORDERS]->(o:Order)<-[r1:ORDERS]-(n1:Customer)
WITH head(collect([n,n1])) as nodes
CALL apoc.refactor.mergeNodes(nodes,{properties:"combine",mergeRels:true}) YIELD node
RETURN node
我使用的是 neo4j 4.2.4 版,以下是我的示例测试数据;
CREATE (c1:Customer {name: "Charlize Theron"})
CREATE (c2:Customer {email: "char@theron.com"})
CREATE (o:Order {ID: 1234})
MERGE (c1)-[:ORDERS]->(o)
MERGE (c2)-[:ORDERS]->(o)
MERGE (c2)-[:LIKES]->(o)
参考:https://neo4j.com/labs/apoc/4.2/graph-updates/graph-refactoring/merge-nodes/
任何人都可以帮助我
MATCH(n:Customer)-[r:ORDERS]->(o:Order)<-[r1:ORDERS]-(n1:Customer)
WITH COLLECT(n)+COLLECT(n1) as nodes
CALL apoc.refactor.mergeNodes(nodes,{mergeRels:true}) YIELD node
RETURN node
我实际上想合并 n 和 n1,因为假设 n 有“姓名”属性,n2 有“电子邮件”属性。我试过上面的代码,它说:“Neo.ClientError.Schema.ConstraintValidationFailed”,解释是:“不能删除节点<1397>,因为它仍然有关系。要删除这个节点,你必须先删除它的关系。”
我找到了解决方案:
//SOLUTION
MATCH(n:Customer)-[r:ORDERS]->(o:Order)<-[r1:ORDERS]-(n1:Customer)
WITH o.order_id as i, HEAD(COLLECT([n,n1])) AS nodes
CALL apoc.refactor.mergeNodes(nodes, {mergeRels:true})
YIELD node
RETURN node
它适用于所有称为 n 和 n1 的节点
不要使用 collect(n) + collect(n1),而是使用 head(collect[n, n1]))。此外,您应该在参数中包含组合属性。
OLD:
MATCH(n:Customer)-[r:ORDERS]->(o:Order)<-[r1:ORDERS]-(n1:Customer)
WITH COLLECT(n)+COLLECT(n1) as nodes
CALL apoc.refactor.mergeNodes(nodes,{mergeRels:true}) YIELD node
RETURN node
New:
MATCH(n:Customer)-[r:ORDERS]->(o:Order)<-[r1:ORDERS]-(n1:Customer)
WITH head(collect([n,n1])) as nodes
CALL apoc.refactor.mergeNodes(nodes,{properties:"combine",mergeRels:true}) YIELD node
RETURN node
我使用的是 neo4j 4.2.4 版,以下是我的示例测试数据;
CREATE (c1:Customer {name: "Charlize Theron"})
CREATE (c2:Customer {email: "char@theron.com"})
CREATE (o:Order {ID: 1234})
MERGE (c1)-[:ORDERS]->(o)
MERGE (c2)-[:ORDERS]->(o)
MERGE (c2)-[:LIKES]->(o)
参考:https://neo4j.com/labs/apoc/4.2/graph-updates/graph-refactoring/merge-nodes/