我正在使用 Neo4j 5.9.0 社区
问题描述: 我的图只有一种类型的关系 [:Relationship] 和一种类型的节点 (:Node)。 我想找到两个节点之间的最短路径,但我不希望返回的最短路径包含此模式:(:Node)<-[:Relationship]-(:Node)-[:Relationship]->(:Node)
我已经读过这里那个
如果shortestPath函数的MATCH子句中包含WHERE子句,则如果存在则返回满足WHERE子句条件的最短路径。
所以这应该是可能的,并且我已经在不使用最短路径函数的情况下进行了测试,验证此要求的路径确实存在于我的图中。
我尝试过的: 我测试了多种方法来过滤返回的最短路径,但没有一个成功。 我尝试使用 exists{} 子查询:
match p = shortestPath((startNode:Node {id : 1})-[:Relationship*]-(endNode:Node {id : 2}))
where none(pathNode in nodes(p) where exists{
match (n1:Node)<-[:Relationship]-(pathNode)-[:Relationship]->(n2:Node)
where ((n1 in nodes(p)) and (n2 in nodes(p)))
})
return p
返回“转换期间未知的表达式类型(类 org.neo4j.cypher.internal.ir.ast.ExistsIRExpression)”,
以及使用 isEmpty() 谓词函数:
match p = shortestPath((startNode:Node {id : 1})-[:Relationship*]-(endNode:Node {id : 2}))
where isEmpty ([(n1)<-[:Relationship]-(n2)-[:Relationship]->(n3) where ((n1 in nodes(p)) and (n2 in nodes(p)) and (n3 in nodes(p))) | n2.id])
return p
返回“转换期间未知的表达式类型(类 org.neo4j.cypher.internal.ir.ast.ListIRExpression)”
我不明白为什么上面的查询不能正常工作。任何帮助将不胜感激,谢谢。
5.6 版本中存在与 EXISTS 子查询有些相关的问题,本应在 Neo4j 5.11 中修复。事实上,我测试了 5.11 中该问题提供的示例查询,它没有生成任何错误。但该查询不涉及 shortestPath
函数。但是,您的 2 个查询在我看来是合理的,但在 5.11 中继续导致相同的错误。该问题似乎与
shortestPath
函数的处理有关。如果将每个查询的第一个子句替换为
MATCH (startNode:Node {id : 1})-[:Relationship*]-(endNode:Node {id : 2})
,错误就会消失。您应该为
shortestPath
函数创建一个 new issues
,并使用您的 2 个查询(或更简单的查询)作为示例。
问题后,Neo4j 团队解决了这个错误,并声明它已在 5.15 版本中修复。我没有亲自验证或测试过这一点。