Neo4j / Cypher - 查找路径但此路径的中间节点不能为 X 类型

Neo4j / Cypher - find path but middle nodes of this path can't be of type X

我有这段寻找最短路径的代码:

p=shortestPath((startNode)-[*..10]-(endNode))

现在我想补充一个条件,中间节点的none个可以是X类型。只有第一个节点具有 X 类型。

我觉得是这样的:

MATCH p=shortestPath((startNode)-[*..10]-(endNode))
WHERE none(node IN nodes(p)[1..-1] WHERE node:X)

关键是从过滤器中省略第一个和最后一个节点,如下所示:

nodes(p)[1..-1]

这值得一读,以尽量避免 Cypher 退回到慢速算法

https://neo4j.com/docs/cypher-manual/current/execution-plans/shortestpath-planning/