我正在对电影数据库进行查询,以检查n个节点之间的最短路径。在这个简化的例子中,我们想要2部电影之间的所有最短路径:
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;
然而,这没有给我任何结果。我误以为它只会回到那些之间没有基努·里维斯的路径:
(no changes, no records)
我测试过我是否只能通过any()
函数得到Keanu Reeves。这非常有效:
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