如何计算 Neo4j 中浮点值的排名?

How to calculate rank for float values in Neo4j?

我正在使用 apoc.algo.dijkstra 计算一组路径。我的目标是为每个建议路径分配一个等级。重要的是节点之间的所有权重都是浮点数。密码:

 ...
 WITH origin, target CALL apoc.algo.dijkstra(origin, target, 'link', 
 'Weight') yield path as path, weight as weight
 ...

我现在拥有的:

Path 1 - Weight: 1.2344332423
Path 2 - Weight: 0.8432423321
Path 3 - Weight: 0.9144331653

我需要的是:

rank: 1, weight: 1.2344332423
rank: 2, weight: 0.9144331653
rank: 3, weight: 0.8432423321

如何在 Cypher 查询中执行此操作。

注意:我已经阅读了有关计算排名的post,但它不适合我的具体情况。

How to calculate a rank in Neo4j

谢谢!

附加信息: 我现在正在尝试将 ranking 和 wight 值与 origin 和 path 合并。我可以为 origin 成功地做到这一点:

 CALL 
 apoc.load.json("file:///.../input.json") YIELD value 
 UNWIND value.origin AS orig 
 MATCH(origin:concept{name:orig.label}) WITH value, collect(origin) as 
 origins 
 UNWIND value.target AS tar MATCH(target:concept{name:tar.label}) 
 UNWIND origins AS origin WITH origin, target 
 CALL apoc.algo.dijkstra(origin, target, 'link', 'Weight') yield path as 
 path, weight as weight 
 WITH origin, path, weight ORDER BY weight ASC WITH {origin: origin, weight: 
 collect(weight)} AS SuggestionForOrigin UNWIND [r in range(1, 
 SIZE(SuggestionForOrigin.weight)) | {origin: SuggestionForOrigin.origin, 
 rank:r, weight: SuggestionForOrigin.weight[r-1]}] AS suggestion RETURN 
 suggestion

然后我得到以下结果(这让我很满意):

 {"origin": {"name": "A","type": "string"},"rank": 1,"weight": 0.0}
 {"origin": {"name": "A","type": "string"},"rank": 2,"weight": 
 0.6180339887498948}
 {"origin": {"name": "P1","type": "string"},"rank": 1,"weight": 
 0.6180339887498948}
 {"origin": {"name": "P1","type": "string"},"rank": 2,"weight": 
 1.2360679774997896}

但是当我尝试合并 "path" 参数时,我遇到了麻烦。我想,我过度补偿了这些东西。我想要实现的目标是:

 {"origin": {....}, "path": {...}, "rank": 1,"weight": 0.0}

而且这需要与特定的源节点相关,如果我有第一个源的3条路径建议,则需要将它们组合在一起。我#ve 尝试过,但它没有像我想要的那样工作是:

 ...
 CALL apoc.algo.dijkstra(origin, target, 'link', 'Weight') yield path as 
 path, weight 
 WITH {origin: origin, path: collect(path), weight: collect(weight)} AS 
 SuggestionForOrigin 
 UNWIND [r in range(1, SIZE(SuggestionForOrigin.weight)) | {rank:r, weight: 
 SuggestionForOrigin.weight[r-1], path: SuggestionForOrigin}] AS suggestion 
 WITH {origin: SuggestionForOrigin.origin, suggestions: collect(suggestion) 
 [0..3]} AS output 
 RETURN output

如果你能提供帮助,我将不胜感激。

这可能对你有用:

...
WITH origin, target
CALL apoc.algo.dijkstra(origin, target, 'link', 'Weight') YIELD weight
WITH weight
ORDER BY weight DESC
WITH COLLECT(weight) AS ws
UNWIND [r IN RANGE(1, SIZE(ws)) | {rank:r, weight: ws[r-1]}] AS res
RETURN res;

结果(假设您的示例数据)如下所示:

╒════════════════════════════════╕
│"res"                           │
╞════════════════════════════════╡
│{"rank":1,"weight":1.2344332423}│
├────────────────────────────────┤
│{"rank":2,"weight":0.9144331653}│
├────────────────────────────────┤
│{"rank":3,"weight":0.8432423321}│
└────────────────────────────────┘

[更新]

如果您还想 return originpath(并按权重升序排列),请参阅