合并neo4j中的现有记录,删除重复项,保持关系

时间:2014-08-14 19:16:42

标签: graph neo4j cypher

出于性能原因,我使用CREATE导入了数百万条记录,现在我想将MERGE记录放在一起,并保持所有关系完整。

有什么想法吗?

编辑:

MATCH (c1:company), (c2:company) 
WITH c1, c2 
WHERE c1.name = c2.name 
SET c1=c2

我正在寻找的东西类型。

2 个答案:

答案 0 :(得分:4)

如果要在cypher中合并节点,可以执行以下操作:

MATCH (c:Company)
WITH c.name as name, collect(c) as companies, count(*) as cnt
WHERE cnt > 1
WITH head(companies) as first, tail(companies) as rest
LIMIT 1000
UNWIND rest AS to_delete
MATCH (to_delete)<-[r:WORKS_AT]-(e:Employee)
MERGE (first)<-[:WORKS_AT]-(e)
DELETE r
DELETE to_delete
RETURN count(*);

请参阅:http://www.neo4j.org/graphgist?dropbox-14493611%2Fmerge_nodes.adoc

答案 1 :(得分:1)

它不起作用。无法移动关系,也无法合并现有节点。您应该从一开始就使用MERGE,以及约束和索引来帮助提高性能。