从链接列表中删除多个项目

时间:2016-05-07 12:47:05

标签: neo4j cypher

我有以下节点列表:

MATCH events=(begin)-[:NEXT*]->(end)
WHERE id(begin)=175 AND id(end)=93
RETURN events

List of nodes

它们被标记为PinkGray

我的问题是:如何删除粉红色节点?

我尝试了以下内容:

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]已删除,无法用于创建关系

我或多或少地了解发生了什么,但我不知道如何解决它。

1 个答案:

答案 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})

enter image description here

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

检查结果:

enter image description here