Уникальные ограничения на свойства узла после факта

У меня есть сайт в бета-версии, и он включает в себя узлы под названием темы. Я забыл добавить уникальное ограничение для свойства name, и теперь есть дубликаты узлов. Я использовал MERGE, надеясь, что он найдет уникальные имена и просто добавит отношения, но, очевидно, это не так. Есть ли в любом случае объединить все отношения в один узел, а затем удалить дубликаты, чтобы я мог добавить ограничение после его очистки?

Я нашел это, но у меня есть несколько узлов с различными отношениями. Кто-нибудь знает, как изменить его для работы в моей ситуации?

//Редактировать

Используя код Майкла, я изменил блок так:

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] |"

Что мне не хватает?

0

Решение

Вы можете немного изменить его, чтобы иметь дело с большим количеством ссылок:

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

Другие решения

У меня была похожая проблема (я думаю) — смотрите мой вопрос здесь. Хотя на мой вопрос не было ответа, техника и вопросы, которые я изложил, действительно решили мою проблему. Это вообще полезно?

Запросы, которые я использовал, как изложено, были похожи на

// 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 …

0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector