我有以下节点列表:
MATCH events=(begin)-[:NEXT*]->(end)
WHERE id(begin)=175 AND id(end)=93
RETURN events
它们被标记为Pink
或Gray
。
我的问题是:如何删除粉红色节点?
我尝试了以下内容:
MATCH (begin)-[:NEXT*]->(toDelete:Pink)-[:NEXT*]->(end)
WHERE id(begin)=175 AND id(end)=93
WITH toDelete
MATCH (prev)-[r1:NEXT]->(toDelete)-[r2:NEXT]->(next)
CREATE (prev)-[:NEXT]->(next)
DELETE r1,r2,toDelete
但是我收到了一个错误:
节点[178]已删除,无法用于创建关系
我或多或少地了解发生了什么,但我不知道如何解决它。
答案 0 :(得分:4)
输入数据:
MATCH (DD:Event) DETACH DELETE DD
MERGE (a:Event:Gray {name:1})-[:Next]->(b:Event:Pink {name:2})
-[:Next]->(c:Event:Gray {name:3})-[:Next]->(d:Event:Pink {name:4})
-[:Next]->(e:Event:Pink {name:5})-[:Next]->(f:Event:Gray {name:6})
-[:Next]->(g:Event:Gray {name:7})
// Get path
MATCH events = (A:Event {name:1})-[:Next*]->(B:Event {name:7})
//
// Separate the gray nodes from pink nodes
WITH nodes(events) as nodes
WITH filter(node in nodes WHERE "Pink" IN LABELS(node)) as pinks,
filter(node in nodes WHERE "Gray" IN LABELS(node)) as grays
//
// Delete pinks
FOREACH(pink in pinks | DETACH DELETE pink)
//
// Let's go through the gray nodes (without the latter)
WITH grays
UNWIND RANGE(0,size(grays)-2) as i
//
// We find in the way of neighboring pairs between which there is no connection
MATCH (c:Event), (n:Event)
WHERE id(c) = id(grays[i]) AND id(n) = id(grays[i+1]) AND NOT (c)-[:Next]->(n)
//
// Create the missing relations
MERGE (c)-[r:Next]->(n)
检查结果: