0

This issue is directly related to the previous post:

How to calculate rank for float values in Neo4j?

I am trying to merge the "rank" and "weight" values with origin and path. I could successfully 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 (not exactly, but to be able to combine "path" with appropriate "weight"):

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

1

This should work:

...
CALL apoc.algo.dijkstra(origin, target, 'link', 'Weight') YIELD path, weight
WITH origin, path, weight
ORDER BY weight
WITH origin, COLLECT(path) AS ps, COLLECT(weight) AS ws
UNWIND [r IN RANGE(1, SIZE(ws)) | {
  origin: origin,
  path: ps[r-1],
  rank: r,
  weight: ws[r-1]}] AS res
RETURN res;
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • Thank you, you helped me a lot, amazing solution! – Artem Nazarenko Jun 25 '19 at 22:04
  • You already helped me a lot, but I am sorry again, I would appreciate, if you could have a look into: https://stackoverflow.com/questions/56782497/combining-the-the-records-within-output-in-relation-to-the-source-in-neo4j – Artem Nazarenko Jun 27 '19 at 00:34
  • 1
    Thank you, today I solved the problem. I was too tired and the last question doesn't make a lot of sense. I will remove it. If I could vote more than once for your responses I would do this. Thank you again! – Artem Nazarenko Jun 27 '19 at 20:23