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

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

问题描述

我对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)

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

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天全站免登陆