合并重复节点及其关系

时间:2018-12-04 16:01:54

标签: neo4j cypher

我需要合并重复的节点并保留一个副本。我面临的问题是,当我合并节点时,将创建重复的关系。相反,我也希望合并该关系而不重复。

您能给些建议吗?

CREATE (n:People { name: 'Person1', lastname: 'Person1LastName', email_ID:'Person1@test2.com' })
CREATE (n:People { name: 'Person2', lastname: 'Person2LastName', email_ID:'Person2@test2.com' })
CREATE (n:People { name: 'Person2', lastname: 'Person2LastName', staysin:'California' })
CREATE (n:People { name: 'Person3', lastname: 'Person3LastName', email_ID:'Person3@test2.com' })

Person2  -[r:Has_Met]->(Person1)

(Person3)-[r:FRIENDS_WITH]->(Person2) having email_ID='Person2@test2.com'

现在,我想保留Person2节点,并保持与其他节点的关系- 像这样的东西:

MATCH (p:People{name:"person1"})
WITH p.name as name, collect(p) as nodes, count() as cnt
WHERE cnt > 1
WITH head(nodes) as first, tail(nodes) as rest
UNWIND rest AS to_delete
MATCH (to_delete)-[r:HAS_MET]->(e:name)
MERGE (first)-[r1:HAS_MET]->(e)
on create SET r1=r
SET to_delete.isDuplicate=true
RETURN count();

This is a related question,但是在这里,我知道将只考虑一种关系(HAS_MET)。我如何一次考虑所有关系?

1 个答案:

答案 0 :(得分:0)

不幸的是,如果没有您的模型演示或没有示例数据列表,我一般只能回答,但是仍然可以为您提供帮助。 看一下APOC library并考虑使用过程Merge NodesRedirect Relationship To。您会在每种情况下找到说明性图像和Cypher语句。


问题更新后的扩展名

初始情况

CREATE
  (p1:People {name: 'Person1', lastname: 'Person1LastName', email_ID: 'Person1@test2.com'}),
  (p2a:People {name: 'Person2', lastname: 'Person2LastName', email_ID: 'Person2@test2.com'}),
  (p2b:People {name: 'Person2', lastname: 'Person2LastName', staysin: 'California'}),
  (p3:People {name: 'Person3', lastname: 'Person3LastName', email_ID: 'Person3@test2.com'}),
  (p2a)-[:HAS_MET]->(p1),
  (p2b)-[:HAS_MET]->(p1),
  (p3)-[:FRIENDS_WITH]->(p2a);

graph

解决方案

MATCH (oneNode:People {email_ID: 'Person2@test2.com'}), (otherNode:People {staysin: 'California'})
CALL apoc.refactor.mergeNodes([oneNode, otherNode])
YIELD node
MATCH (node)-[relation:HAS_MET]->(:People)
WITH tail(collect(relation)) AS surplusRelations
UNWIND surplusRelations AS surplusRelation
DELETE surplusRelation;
  • 第1行:同时选择要合并的节点
  • 第2行:调用适当的合并节点过程
  • 第3行:定义结果变量
  • 第4行:确定组合节点与遇见者之间的所有关系(至少有两个)
  • 第5行:选择除第一个关系之外的所有关系
  • 第7行:删除所有剩余关系

结果

  • 合并节点Person2,其中包含源节点的所有属性(特别注意email_IDstaysin
  • 一个关系Person1-Person2

graph2