How to calculate rank for float values in Neo4j?

2019-08-17 08:39发布

问题:

I am calculating a set of paths using apoc.algo.dijkstra. My goal is to assign a rank to each of the suggested paths. Important is all the weights among nodes are floats. Cypher code:

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

what I have now:

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

Something what I need is:

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

How can I do this inside the Cypher query.

Note: I already read the post related to calculating the rank, but it doesn't suit to my specific case.

How to calculate a rank in Neo4j

Thank you!

Additional info: I am trying now to merge the ranking and wight values with origin and path. I could succesully do this for 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

Then I get the following result (which is satisfying for me):

 {"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}

But when I am trying to merge "path" parameter, I am getting into trouble. I think, I overcompensate the things. Something what I would like to achieve is:

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

And this need to be related to a particular origin node, if I have 3 paths suggestions for the first origin, they need to be combined together. What I#ve tried, but it doesn't work as I want is:

 ...
 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

I would appreciate, if you could help.

回答1:

This may work for you:

...
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;

The result (assuming your sample data) would look like this:

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

[UPDATE]

If you want to also return the origin and path (and rank by ascending weight), see this answer.