构建复杂的输出并对Neo4j中的记录进行排名 [英] Building a complex output and ranking the records in Neo4j

查看:117
本文介绍了构建复杂的输出并对Neo4j中的记录进行排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与上一篇文章直接相关:

This issue is directly related to the previous post:

如何在Neo4j中计算浮点值的排名? /a>

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}

但是当我尝试合并"path"参数时,我遇到了麻烦.我认为,我对这些东西补偿过高.我想实现的目标是(不完全是,但是能够将路径"与适当的重量"结合起来):

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}

这需要与特定的原始节点有关,如果我对第一个原始节点有3条路径建议,则需要将它们组合在一起.我已经尝试过,但是按我的意愿却无法正常工作:

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.

推荐答案

这应该有效:

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

这篇关于构建复杂的输出并对Neo4j中的记录进行排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆