Neo4j shortestPath reduce函数返回null

时间:2016-06-10 06:46:23

标签: neo4j cypher shortest-path

我正在尝试在Neo4j中使用ShortestPath功能。

路由将通过具有LinkLength值的不同节点。对于2个节点之间的最短LinkLength,“reduce”部分中的值应该最小化。

问题: Neo4j可以找到一条路径作为最短路径解决方案。令人惊讶的是,Neo4j表示reduce函数的值为null。什么是错误?

MATCH p = (n1:Node)-[:Connects*]->(n2:Node)
WHERE n1.myid = 'M32 J3' AND n2.myid = 'M32 J1'
RETURN p AS shortestPath,
reduce(distance=0.0, n in nodes(p) | case n.LinkLength when NOT NULL then
distance+toFloat(n.LinkLength) end)
LIMIT 1;

1 个答案:

答案 0 :(得分:0)

您的case声明没有其他部分。不是100%肯定,但我想在这种情况下,您将返回null作为累加器值 - null + 1 == null

正确的方法是首先过滤掉具有空值的节点,然后再应用reduce

MATCH p = (n1:Node)-[:Connects*]->(n2:Node)
WHERE n1.myid = 'M32 J3' AND n2.myid = 'M32 J1'
RETURN p AS shortestPath,
reduce(distance=0.0, n in [x in nodes(p) WHERE n.LinkLength IS NOT NULL] |
   distance+toFloat(n.LinkLength))
LIMIT 1;

此外,我猜您基本上想要计算最短加权路径。您的陈述只是获得了第一条路径的权重。为了有效地进行加权最短路径,请查看https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_graph_algorithms_work_in_progress。 (记住:apoc需要Neo4j> = 3.0)。

相关问题