我需要合并重复的节点并保留一个副本。我面临的问题是,当我合并节点时,将创建重复的关系。相反,我也希望合并该关系而不重复。
您能给些建议吗?
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)。我如何一次考虑所有关系?
答案 0 :(得分:0)
不幸的是,如果没有您的模型演示或没有示例数据列表,我一般只能回答,但是仍然可以为您提供帮助。 看一下APOC library并考虑使用过程Merge Nodes和Redirect 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);
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;
email_ID
和staysin
)