不包括一个节点的最短路径
Shortest paths excluding one node
我正在对电影数据库进行查询以检查 n 个节点之间的最短路径。在这个简化的例子中,我们想要两部电影之间的所有最短路径:
match p=allShortestPaths((n)-[*]-(m)) where id(n) = 87 and id(m) = 121
return p;
现在我想要所有不包含基努·里维斯的最短路径。
我试过这个:
match p=allShortestPaths((n)-[*]-(m)) where id(n) = 87 and id(m) = 121 and NONE(n in nodes(p) where n.name = "Keanu Reeves")
return p;
然而,这需要很长时间才能加载,即使在我已经为 Person 的名称字段建立索引之后...
然后尝试以下操作:
match p=allShortestPaths((n)-[*]-(m)) where id(n) = 87 and id(m) = 121
with p WHERE NONE(n in nodes(p) where n.name = "Keanu Reeves")
return p;
然而,这没有给我任何结果。我误解了这一点,认为它只是 return 那些中间没有基努·里维斯的路径:
(no changes, no records)
我测试了我是否只能使用 any()
函数得到基努·里维斯中间的那些。这非常有效:
match p=allShortestPaths((n)-[*]-(m)) where id(n) = 87 and id(m) = 121
with p WHERE ANY(n in nodes(p) where n.name = "Keanu Reeves")
return p;
解决这个问题的最佳方法是什么?我必须说我的生产查询比这复杂得多,但这一切都归结为这个问题。它必须是一个高效的解决方案。
问题是,如果路径中的一个节点没有 属性 name
,那么整个检查将不会通过。
所以还是我们检查 the existence of a property:
match p=allShortestPaths((n)-[*]-(m))
where
n.title = 'The Replacements' and
m.title = 'Speed Racer' and
NONE(n in nodes(p) where EXISTS(n.name) and n.name = "Keanu Reeves")
return p
或使用COALESCE
函数:
match p=allShortestPaths((n)-[*]-(m))
where
n.title = 'The Replacements' and
m.title = 'Speed Racer' and
NONE(n in nodes(p) where COALESCE(n.name, '') = "Keanu Reeves")
return p
我正在对电影数据库进行查询以检查 n 个节点之间的最短路径。在这个简化的例子中,我们想要两部电影之间的所有最短路径:
match p=allShortestPaths((n)-[*]-(m)) where id(n) = 87 and id(m) = 121
return p;
现在我想要所有不包含基努·里维斯的最短路径。 我试过这个:
match p=allShortestPaths((n)-[*]-(m)) where id(n) = 87 and id(m) = 121 and NONE(n in nodes(p) where n.name = "Keanu Reeves")
return p;
然而,这需要很长时间才能加载,即使在我已经为 Person 的名称字段建立索引之后...
然后尝试以下操作:
match p=allShortestPaths((n)-[*]-(m)) where id(n) = 87 and id(m) = 121
with p WHERE NONE(n in nodes(p) where n.name = "Keanu Reeves")
return p;
然而,这没有给我任何结果。我误解了这一点,认为它只是 return 那些中间没有基努·里维斯的路径:
(no changes, no records)
我测试了我是否只能使用 any()
函数得到基努·里维斯中间的那些。这非常有效:
match p=allShortestPaths((n)-[*]-(m)) where id(n) = 87 and id(m) = 121
with p WHERE ANY(n in nodes(p) where n.name = "Keanu Reeves")
return p;
解决这个问题的最佳方法是什么?我必须说我的生产查询比这复杂得多,但这一切都归结为这个问题。它必须是一个高效的解决方案。
问题是,如果路径中的一个节点没有 属性 name
,那么整个检查将不会通过。
所以还是我们检查 the existence of a property:
match p=allShortestPaths((n)-[*]-(m))
where
n.title = 'The Replacements' and
m.title = 'Speed Racer' and
NONE(n in nodes(p) where EXISTS(n.name) and n.name = "Keanu Reeves")
return p
或使用COALESCE
函数:
match p=allShortestPaths((n)-[*]-(m))
where
n.title = 'The Replacements' and
m.title = 'Speed Racer' and
NONE(n in nodes(p) where COALESCE(n.name, '') = "Keanu Reeves")
return p