7vingt
7vingt

Reputation: 301

Neo4j / Cypher : order by and where, know the position of the result in the sort

Does it possible to have an order by "property" with a where clause and now the "index/position" of the result?

I mean, when using order for sorting we need to be able to know the position of the result in the sort.

Imagine a scoreboard with 1 million user node, i do an order by on user node.score with a where "name = user_name" and i wan't to know the current rank of the user. I do not find how to do this using order by ...

    start game=node(1)
    match game-[:has_child_user]->user
    with user
    order by user.score
    with user
    where user.name = "my_user"
    return user , "the position in the sort";

the expected result would be :

node_user | rank

(i don't want to fetch one million entries at client side to know the current rank/position of a node in the ORDER BY!)

Upvotes: 3

Views: 813

Answers (1)

p3rnilla
p3rnilla

Reputation: 351

This functionality does not exist today in Cypher. Do you have an example of what this would look like in SQL? Would the below be something that fits the bill? (just a sketch, not working!)

(your code)

start game=node(1)
match game-[:has_child_user]->user
with user
order by user.score

(+ this code)

with user, index() as rank
return user.name, rank; 

If you have more thoughts or want to start hacking on this please open an issue at https://github.com/neo4j/neo4j/issues

For the time being there is a work around that you can do:

start n=node(0),rank_node=node(1) 
match n-[r:rank]->rn 
where rn.score <= rank_node.score 
return rank_node,count(*) as pos;

For live example see: http://console.neo4j.org/?id=bela20

Upvotes: 3

Related Questions