问题
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:
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;
来源:https://stackoverflow.com/questions/56759973/building-a-complex-output-and-ranking-the-records-in-neo4j