Neo4J通过特定节点获得节点之间的最短路径

时间:2020-11-05 14:03:23

标签: neo4j cypher shortest-path

我们有一个Neo4J数据库,该数据库跟踪在公司工作的人员。总体结构是-

(Person)-[:WORKS_AT]->(Company)

一个人可以同时担任多个工作,每个公司可以有多个人。

给出2个公司(公司1和公司2),我们可以使用-计算最短路径

match (start:Company {id:1}),(end:Company {id:2}), p = shortestPath((start)-[:WORKS_AT*]-(end)) return *

我们要做的是计算两家公司之间的最短路径,其中该路径包括特定人员,例如-

(Company 1)-(Person 1)-(Company 4)-(Person 2)-(Company 2)
如果我们想通过Person 2连接,将返回

而不是以下内容-

(Company 1)-(Person 3)-(Company 2)

我尝试在Neo4j Shortest Path for specific node labels in-between the path中实施建议,但返回错误-

最短路径算法在开始和结束节点时不起作用 是相同的。如果执行最短路径搜索,可能会发生这种情况 可能具有相同起点和终点的笛卡尔乘积之后 传递给shortestPath的某些行。如果你不想 遇到此异常,并且可以接受丢失的可能性 这些行的结果,请在Neo4j配置中通过以下方式禁用此功能 将cypher.forbid_shortestpath_common_nodes设置为false。如果你 无法接受缺失的结果,并且确实想要最短的路径 在两个公共节点之间,然后使用标准重写查询 密码变长模式表达,然后按路径排序 长度并限制为一个结果。

编辑:链接的文章错误-应该是shortest path between 2 nodes through waypoints in neo4j

我们尝试运行的查询失败-

match(s:Company{ID:1})
match(f:Company{ID:2})
match (n:Person) where n.id in [2]
with collect(n) as wps 
match path=allshortestPaths((s)-[:WORKS_AT*]->(f))
where ANY ( n in wps WHERE n in nodes(path) ) return *

0 个答案:

没有答案
相关问题