Cypher Order along the way Cost

I am extremely new to cypher and neo4j. I want to get and order all the paths between points A and B, based on the total cost of the path. Value in this case is a property of relations, which is an integer. The value of the path would be a summation of the properties of the relationship.

I am looking at some examples of the cypher ORDER BY operator, however, by the examples, it seems that you need to order a property that is already assigned to the object to be ordered, in this case it will not work, because the paths do not have the static property β€œcost” .

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

I am sure that such things are not too complicated for cypher.

+2
source share
2 answers

I tried and could not do something very similar to this a few days ago. The problem is that you cannot get the amount of the collection, but only the amount of aggregation.

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

I hope they add some features to this in Cypher, but I could not get it to work, even with the help of an expert, Michael Hunder.

UPDATE . I actually in the course of Cypher code today made an expression that does just that. I’m not sure that it will be adopted at 1.9, but perhaps some form of it will soon enter the community.

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

Basically this is what you ask for - a slightly outdated version of my data is here: http://console.neo4j.org/r/2rvznu

And here is Cypher ( NOTE, 1.9-SNAPSHOT is currently required ):

  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; 

gives:

 +------------------------------------------+ | 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 | +------------------------------------------+ 
+8
source

For the convenience of all users in this question, I updated @ eve-freeman's answer on Neo4j 3.0

 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; 

The differences are as follows: Parentheses are needed to identify nodes in the Pipe | instead of a colon: used to separate a "variable in a list" and an expression in functions

+1
source

Source: https://habr.com/ru/post/925534/


All Articles