事后对节点属性的唯一约束

时间:2015-11-12 21:04:47

标签: php neo4j cypher

我有一个测试版的网站,它包含名为Topics的节点。我忘了将唯一约束添加到name属性,现在有重复的节点。我使用MERGE希望它会找到唯一的名称,只是添加关系,但显然情况并非如此。有没有将所有关系合并到一个节点然后删除重复项,以便我可以在清理后添加约束?

我找到了这个,但我有多个节点有不同的关系。任何人都知道如何修改它以适应我的情况?

//修改

使用Michael的代码,我将块修改为:

MATCH (t:Topic)
WITH t.name as name, collect(t) as topics, count(*) as cnt
WHERE cnt > 1
WITH head(topics) as first, tail(topics) as rest
LIMIT 1000
UNWIND rest AS to_delete
OPTIONAL MATCH (to_delete)-[r:TOPIC_OF]->(g:Group)
FOREACH (x in case when r is null then [] else [1] |
   MERGE (first)-[new_r:TOPIC_OF]->(g)
   // in case you have to copy-rel-properties
   SET new_r = r
   DELETE r
)
OPTIONAL MATCH (to_delete)<-[r2:INTEREST_OF]-(p:Person)
FOREACH (x in case when r2 is null then [] else [1] |
   MERGE (first)-[new_r2:INTEREST_OF]->(p)
   // in case you have to copy-rel-properties
   SET new_r2 = r2
   DELETE r2
)

OPTIONAL MATCH (to_delete)<-[r3:SKILL_OF]-(p:Person)
FOREACH (x in case when r3 is null then [] else [1] |
   MERGE (first)-[new_r3:SKILL_OF]->(p)
   // in case you have to copy-rel-properties
   SET new_r3 = r3
   DELETE r3
)

OPTIONAL MATCH (to_delete)-[r4:TOPIC]->(p:Project)
FOREACH (x in case when r4 is null then [] else [1] |
   MERGE (first)-[new_r4:TOPIC]->(p)
   // in case you have to copy-rel-properties
   SET new_r4 = r4
   DELETE r4
)

OPTIONAL MATCH (to_delete)-[r5:NEEDS]->(p:Project)
FOREACH (x in case when r5 is null then [] else [1] |
   MERGE (first)-[new_r5:NEEDS]->(p)
   // in case you have to copy-rel-properties
   SET new_r5 = r5
   DELETE r5
)
DELETE to_delete
RETURN count(*);

我在运行时遇到错误:

Invalid input '|': expected whitespace, comment, '.', node labels, '[', "=~", IN, STARTS, ENDS, CONTAINS, IS, '^', '*', '/', '%', '+', '-', '=', "<>", "!=", '<', '>', "<=", ">=", AND, XOR, OR or END (line 8, column 52 (offset: 276))
"FOREACH (x in case when r is null then [] else [1] |"

我错过了什么?

2 个答案:

答案 0 :(得分:1)

您可以稍微修改它以处理更多相关信息:

MATCH (t:Topic)
WITH t.name as name, collect(t) as topics, count(*) as cnt
WHERE cnt > 1
WITH head(topics) as first, tail(topics) as rest
LIMIT 1000
UNWIND rest AS to_delete
OPTIONAL MATCH (to_delete)-[r:TOPIC_OF]->(g:Group)
FOREACH (x in case when r is null then [] else [1] |
   MERGE (first)-[new_r:TOPIC_OF]->(g)
   // in case you have to copy-rel-properties
   SET new_r = r
   DELETE r
)
OPTIONAL MATCH (to_delete)-[r2:TOPIC_OF2]->(g:Group)
FOREACH (x in case when r is null then [] else [1] |
   MERGE (first)-[new_r2:TOPIC_OF2]->(g)
   // in case you have to copy-rel-properties
   SET new_r2 = r2
   DELETE r2
)
DELETE to_delete
RETURN count(*);

答案 1 :(得分:0)

我有类似的问题(我认为) - 请参阅my question here。虽然我的问题没有得到解答,但我提出的技术和问题确实解决了我的问题。这有用吗?

我按照概述使用的查询类似于

// get all outgoing relationships
MATCH (a:Label1 { title : 'blah' })-[r]->(o)
RETURN r
// returns FOO and BAR

// for each relationship type, create one from (d) and copy the properties over
MATCH (a:Label1 { title : 'blah' })-[r:FOO]->(o), (d:Label1 { title : 'blah blah' })
CREATE (d)-[r2:FOO]->(o)
SET r2 = r
...etc...

// now do the same for incoming relationships
MATCH (a:Label1 { title : 'blah' })<-[r]-(o)
RETURN r
// returns FOO and BAR

// for each relationship type, create one from (d) and copy the properties over
MATCH (a:Label1 { title : 'blah' })<-[r:FOO]-(o), (d:Label1 { title : 'blah blah' })
CREATE (d)<-[r2:FOO]-(o)
SET r2 = r
...etc...

// finally delete node and relationships (if required)
MATCH (a:Label1 { title : 'blah' })-[r]-(o)
DELETE r, a

当然,只有您可以单独识别由a和d ...

标识的节点时,这才有用
相关问题