Cypher:在同一查询中找不到节点

时间:2016-03-31 01:42:12

标签: path neo4j cypher

我有这个测试数据库:

enter image description here

我想删除右侧有两个+B个节点的路径。这种情况通常可以描述为一个路径,其中包含PS个节点(+B个节点也是PS个节点,它们没有传入的:SOURCE边缘。我想要删除的是(writer)(不包括)和没有传入:SOURCE边缘(包括)的节点之间的那个。

为此,我有这个问题:

MATCH p1=(writer:A {type:'writer'})-[*]->(Q:PS)-[:TARGET*]->(T)
WITH (Q), (writer)
MATCH (Q)
WHERE NOT ()-[:SOURCE]->(Q)
WITH (Q), (writer)

MATCH p2=(writer)-[*]->(Q)
WHERE ANY (x IN NODES(p2)[1..] WHERE x:PS AND NOT ()-[:SOURCE]->(x))

WITH REDUCE(s = [], y IN NODES(p2)[1..] | CASE
  WHEN y:PS THEN s + y
  ELSE s END) AS todo
FOREACH (z IN todo | DETACH DELETE z);

它首先识别所述节点,然后将它们传递给新的路径选择,该路径选择以该节点结束。一切正常。什么是行不通的是从WITH REDUCE开始的最后一部分。它表示找不到Q,但Q甚至不会出现在该部分中。

错误为Node <some ID> not found。这是为什么?为什么它没有再次找到节点,为什么它甚至试图在最后一部分?关闭最后一部分,查询按照预期工作。

1 个答案:

答案 0 :(得分:1)

[增订]

该错误通常意味着您尝试删除已删除的节点,或删除具有已删除的端点节点的关系。

尝试使用以下代码替换您的FOREACH子句,在尝试删除它们之前删除重复的节点,并删除节点之前的关系:

UNWIND todo AS node
WITH DISTINCT node
MATCH (node)-[r]-()
WITH COLLECT(DISTINCT node) AS nodes, COLLECT(DISTINCT r) AS rels
FOREACH(r IN rels | DELETE r)
FOREACH(n IN nodes | DELETE n);

此外,您的查询似乎非常低效。这是一个更简单的版本,其中包括上述修复:

MATCH p=(:A {type:'writer'})-[*]->(:PS)-[:TARGET]->()
WHERE ANY (x IN NODES(p)[1..-1] WHERE x:PS AND NOT ()-[:SOURCE]->(x))
WITH REDUCE(s = [], y IN NODES(p)[1..] | CASE
  WHEN y:PS THEN s + y
  ELSE s END) AS todo
UNWIND todo AS node
WITH DISTINCT node
MATCH (node)-[r]-()
WITH COLLECT(DISTINCT node) AS nodes, COLLECT(DISTINCT r) AS rels
FOREACH(r IN rels | DELETE r)
FOREACH(n IN nodes | DELETE n);