按路径成本的密码顺序 [英] Cypher Order by Path Cost

查看:15
本文介绍了按路径成本的密码顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 cypher 和 neo4j 非常陌生.我想根据总路径成本获取和排序 A 点和 B 点之间的所有路径.在这种情况下,成本是一个整数关系属性.路径成本将是关系属性的总和.

I am extremely new to cypher and neo4j. I am wanting to get and order all paths between point A and B, based upon the total path cost. The cost in this case is a relationship property that is an integer. The path cost would be a summation of the relationship properties.

我正在查看 cypher 的 ORDER BY 语句的一些示例,但是,通过示例,您似乎必须按已分配给正在排序的对象的属性进行排序,在这种情况下,这将不起作用,因为路径没有静态的成本"属性.

I am looking at some examples of cypher's ORDER BY statement however, by the examples, it seems that you have to order by a property that is already assigned to the object being ordered, in this case that won't work because paths do not have a static "cost" property.

(顺便说一句,这与路径的长度/数量不同)

(This is different from the lengths/number of paths btw)

我很确定这样的事情对于 cypher 来说并不太复杂.

I am pretty sure things like this are not too complicated for cypher.

推荐答案

几天前我尝试过但未能做与此非常相似的事情.问题在于你不能得到一个集合的总和,只能得到一个聚合的总和.

I tried and failed to do something very similar to this a few days ago. The problem lies in the fact that you can't get a sum of a collection, only the sum of an aggregation.

见:https://groups.google.com/d/topic/neo4j/_MqwGp1a1Oo/discussion

我希望他们能在 Cypher 中为此添加一些功能,但即使在专家 Michael Hunger 的帮助下,我也无法让它工作.

I hope they'll add some functionality for this in Cypher, but I couldn't get it to work, even with the help of the expert--Michael Hunger.

更新我今天实际上在 Cypher 代码中戳了一下,以创建一个完全执行此操作的表达式.我不确定它是否会被 1.9 接受,但也许它的某种形式很快就会进入社区版.

UPDATE I actually poked at the Cypher code today to make an expression that does exactly this. I'm not sure if it will be accepted by 1.9, but maybe some form of it will get into the community edition soon.

更新 2 他们已将我的 reduce 拉取请求合并到 1.9-SNAPSHOT,我将更新下面的语法.

UPDATE 2 They've merged in my pull request for reduce into 1.9-SNAPSHOT, I'll update the syntax below.

它基本上完全符合您的要求——我的数据的一个稍微陈旧的版本在这里:http://console.neo4j.org/r/2rvznu

It does basically exactly what you're asking for--a slightly stale version of my data is here: http://console.neo4j.org/r/2rvznu

这是 Cypher(注意,目前需要 1.9-SNAPSHOT):

And here's the Cypher (NOTE, currently requires 1.9-SNAPSHOT):

   START n=node(18) 
   MATCH p=n-[r*]->m 
   WHERE not(m-->()) 
    WITH extract(x in r: x.score) as scores, length(p) as len
  RETURN scores, reduce(res=0, x in scores: res + x) as totalscore, len 
ORDER BY totalscore desc;

给出:

+------------------------------------------+
| scores        | totalscore         | len |
+------------------------------------------+
| [0.9,0.9,3.7] | 5.5                | 3   |
| [0.8,0.79]    | 1.59               | 2   |
| [0.4,0.75]    | 1.15               | 2   |
| [0.4,0.45]    | 0.8500000000000001 | 2   |
+------------------------------------------+

这篇关于按路径成本的密码顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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