构建复杂的输出并对 Neo4j 中的记录进行排名
Building a complex output and ranking the records in Neo4j
这个问题与之前的post直接相关:
我正在尝试将 "rank" 和 "weight" 值与原点和路径合并。我可以为 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" 参数时,我遇到了麻烦。我想,我过度补偿了这些东西。我想要实现的是(不完全是,但能够将 "path" 与适当的 "weight" 结合起来):
{"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
如果你能提供帮助,我将不胜感激。
这应该有效:
...
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;
这个问题与之前的post直接相关:
我正在尝试将 "rank" 和 "weight" 值与原点和路径合并。我可以为 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" 参数时,我遇到了麻烦。我想,我过度补偿了这些东西。我想要实现的是(不完全是,但能够将 "path" 与适当的 "weight" 结合起来):
{"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
如果你能提供帮助,我将不胜感激。
这应该有效:
...
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;