djveremix / redis

Automatically exported from code.google.com/p/redis
0 stars 0 forks source link

Sorted set has not a same rank even though it has a same score #266

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi, Nice to meet you.

I am thinking that sorted set can be applied to real-time ranking system.
But I knew that the sorted set does not have same rank even thouth they have 
same score.

My questiong is this.
Can I get a same rank when the member have same score?
If that feature is not developed yet, Do you have any plan to add this feature 
as an option ?

I want to get your answer. 

Sorry for my poor english and, 
Thank you very much for your effort on Redis. 

Original issue reported on code.google.com by kikiki.b...@gmail.com on 22 Jun 2010 at 5:08

GoogleCodeExporter commented 8 years ago
I guess you don't understand what the rank of an element in a sorted set means. 
You can also see it as the index of the element within the sorted set. Because 
you cannot have duplicate indices in an array, you cannot have duplicate rank 
for an element in a sorted set. If you need such a thing, you should "ZRANGE 
<key> 0 -1" and perform the special sorting you need on the client side. Good 
luck!

Original comment by pcnoordh...@gmail.com on 22 Jun 2010 at 4:49

GoogleCodeExporter commented 8 years ago
I understand the strict, academic definition of rank, but I don't believe it is 
a useful number to return in this context. I would be happy if you could 
provide me with a counter-example: why would someone using Redis want to know 
the exact index of an element in the sorted set? Aren't we using Redis so that 
we can allow it to deal with that kind of data-structure detail?

I sumbit that it would be more helpful to have a command like kikiki.blue is 
requesting. Something like ZSCORERANK X, which when given element X with score 
Y would return the number of elements with score greater than Y plus one. 
Currently I am hacking this as follows with ZRANGEBYSCORE (node.js code):

redis.zrangebyscore(group_key,score+1,'+inf',function(err,reply){
  var rank = 1;
  if (reply) {
    //redis returns null for no matching elements
    rank = reply.toString().split(',').length + 1;
  }
  ...
})

This is fine, but I'm not looking forward to running this code on sorted sets 
with 100,000+ elements (a totally real use case, I'm using Redis for Facebook 
apps with hundreds of thousands of participants.)

Original comment by ben.nev...@gmail.com on 28 Jul 2010 at 7:38

GoogleCodeExporter commented 8 years ago
You can replace the zrangebyscore and reply parsing with a zcount call, which 
will reduce network IO and client-side parsing overhead.

Also, passing a score+1 won't work when scores are non-integers.  The "+1" 
really should depend on the IEEE FP double epsilon for the particular score 
value, which is really the absolute value divided by 2**53 for non-denormalized 
floats.

That code is easy to write, correct, and fast.

Original comment by josiah.c...@gmail.com on 28 Jul 2010 at 9:55

GoogleCodeExporter commented 8 years ago
Hi josiah,

Thanks for clueing me in to the existence of the ZCOUNT command. It's not 
listed in the documentation of the version of redis I'm using (2.0.0-rc2) .  
I'll investigate!

Yes, you're right -- the score+1 is something specific to my code. For my use 
case I only use integers... about 2^20 < 0 < -(2^20).

Ben

Original comment by ben.nev...@gmail.com on 28 Jul 2010 at 9:59

GoogleCodeExporter commented 8 years ago
You can specify a non-inclusive range for ZRANGEBYSCORE, ZCOUNT, etc, by using 
the left parenthesis character. So, instead of passing "score+1","+inf" (where 
you get x >= score), you can pass "(score","+inf" (where you get x > score). 
This works regardless of precision. The full doc is located here: 
http://code.google.com/p/redis/wiki/ZrangebyscoreCommand

Original comment by pcnoordh...@gmail.com on 29 Jul 2010 at 8:05

GoogleCodeExporter commented 8 years ago
Thanks for the tip about the '(' syntax, pcnoordhuis.

If I wanted to file a bug or report about ZSCORE not being in the 
documentation, where would I do it?  Here?

Original comment by ben.nev...@gmail.com on 29 Jul 2010 at 9:40

GoogleCodeExporter commented 8 years ago
No problem. To keep everything centralized, it's best to report any (valid) 
issue here. ZSCORE documentation is not missing, see: 
http://code.google.com/p/redis/wiki/ZscoreCommand .

Original comment by pcnoordh...@gmail.com on 29 Jul 2010 at 10:33

GoogleCodeExporter commented 8 years ago
Pardon me - the ZSCORE documentation is present in the redis I downloaded. I 
meant ZCOUNT, which I do find at that link, but which is not listed in the 
/doc/CommandReference.html file that came with 2.0.0-rc2 .

Original comment by ben.nev...@gmail.com on 30 Jul 2010 at 3:17

GoogleCodeExporter commented 8 years ago
That was indeed missing, but the docs will be regenerated for every release. 
This means ZCOUNT will be present in doc/ in 2.0 stable.

Original comment by pcnoordh...@gmail.com on 30 Jul 2010 at 7:50