假设我想找到特定类型的某个节点(比如“中央生产单元”)和定义的终端节点(比如带有 id 的“消费者”)之间的最短路径,我如何在 Neo4j 中使用 Cypher 进行计算?
对于此类查询,我想回答以下问题:“哪个生产单位以最短距离为该客户提供食物”。
我尝试使用以下查询:
match p=AllShortestPaths((source:Asset)-[:LINKS_TO*]-(destination:Asset))
where source.type = 'central production unit' and destination.id = '1234'
return extract(n in nodes(p)| n.type) as type_path,
extract(n in nodes(p)| n.id) as id_path,
length(p) as path_length;
像上面这样的查询会遇到内存不足的错误。
使用相同的查询,而不是节点的类型,输入一个特定的id非常好。
在 Stackoverflow 上嗅探我发现了 1 个特定节点到 1 个其他特定节点的几个例子,但还没有 1 个例子来确定某个类型的节点到 1 个特定节点。
答案 0 :(得分:0)
我想我已经找到了使用 sanningTree 程序的解决方案。
这工作得非常快!我不理解为什么。 以及如何包含链接属性以最小化物理属性而不是跃点数。
// first match to and collect end nodes
MATCH (m:Asset {type:'central production unit'})
WITH collect(m) as endNodes
MATCH (n:Asset {id:'1234'})
// proc call will be executed per n node, finding the first shortest path found from n to one of the end nodes
CALL apoc.path.spanningTree(n, {endNodes:endNodes, limit:1}) YIELD path
RETURN path