Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
sorted set get rank by score feature suggestion
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  12 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Karl Seguin  
View profile  
 More options May 31 2011, 11:00 am
From: Karl Seguin <karlseg...@gmail.com>
Date: Tue, 31 May 2011 08:00:56 -0700 (PDT)
Local: Tues, May 31 2011 11:00 am
Subject: sorted set get rank by score feature suggestion

Hi,

I'm in need of getting a rank from a sorted set on arbitrary scores
(unsaved). Currently the API only allows for ranks from a saved member. The
use case that I'm specifically interested in is to return leaderboard ranks
to users when the score isn't their best score (and thus isn't saved). We
only track a user's best score, but we like to show them what rank a score
was - even if its worse than their best rank.

I'm curious if such a patch might be welcomed?

I haven't programmed in C, but I really need this feature and I'm happy to
maintain my own patched fork and run with it. I feel like I have an ok
implementation for skiplists, but I'm struggling with the ziplist
implementation. I'm thinking I'm going to have to iterate though and call
something like zzlGetScore on each entry to find the rank. Any thoughts
suggestion?

here's what I have so far if anyone is interested:
https://github.com/karlseguin/redis/commit/e2a5ac71406178d2e515a1234d...

karl


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pieter Noordhuis  
View profile  
 More options May 31 2011, 11:15 am
From: Pieter Noordhuis <pcnoordh...@gmail.com>
Date: Tue, 31 May 2011 08:15:57 -0700
Local: Tues, May 31 2011 11:15 am
Subject: Re: sorted set get rank by score feature suggestion

Hi Karl,

You can achieve the same thing using already existing commands. You say you want the rank of an arbitrary score. This is equal to knowing the size of the sorted set, and the number of members with a score lower or higher than that score. When you call ZCARD and ZCOUNT in a MULTI/EXEC, you get the size and the number of members from score X to +inf or whatever the range is you are interested in. To find out the rank, you subtract the return value of ZCOUNT from the size and get the relative position of a non-existing element.

Cheers,
Pieter


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Karl Seguin  
View profile  
 More options May 31 2011, 11:19 am
From: Karl Seguin <karlseg...@gmail.com>
Date: Tue, 31 May 2011 08:19:56 -0700 (PDT)
Local: Tues, May 31 2011 11:19 am
Subject: Re: sorted set get rank by score feature suggestion

duh, makes sense...at least it made for a fun evening!!

Karl


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Broglie  
View profile  
 More options Feb 26, 5:22 pm
From: Chris Broglie <cbrog...@gmail.com>
Date: Tue, 26 Feb 2013 14:22:16 -0800 (PST)
Local: Tues, Feb 26 2013 5:22 pm
Subject: Re: sorted set get rank by score feature suggestion

Based on the running time of ZCOUNT, this method would be O(n) where n is
the number of elements in the set. Finding the rank of an arbitrary score
should be able to be implemented in O(log(n)).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josiah Carlson  
View profile   Translate to Translated (View Original)
 More options Feb 27, 6:30 pm
From: Josiah Carlson <josiah.carl...@gmail.com>
Date: Wed, 27 Feb 2013 15:30:03 -0800
Local: Wed, Feb 27 2013 6:30 pm
Subject: Re: sorted set get rank by score feature suggestion
Using Lua scripting, you can perform the following operations with 1
round trip using O(log n) time:

local result = redis.call('zrangebyscore', KEYS[1], ARGV[1], ARGV[1])
if #result > 0 then
    return redis.call('zrank', KEYS[1], result[1])
endif
return -1

 - Josiah


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josiah Carlson  
View profile  
 More options Feb 27, 6:30 pm
From: Josiah Carlson <josiah.carl...@gmail.com>
Date: Wed, 27 Feb 2013 15:30:27 -0800
Local: Wed, Feb 27 2013 6:30 pm
Subject: Re: sorted set get rank by score feature suggestion
P.S. That is completely untested, it may require changes.

On Wed, Feb 27, 2013 at 3:30 PM, Josiah Carlson


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Broglie  
View profile  
 More options Feb 27, 7:19 pm
From: Chris Broglie <cbrog...@gmail.com>
Date: Wed, 27 Feb 2013 16:19:39 -0800 (PST)
Local: Wed, Feb 27 2013 7:19 pm
Subject: Re: sorted set get rank by score feature suggestion

That method requires you to make assumptions about the distribution of the
data contained in the set, b/c you need know what min/max to pass into
ZRANGEBYSCORE to ensure it is a big enough range to include a key, but not
so large that it returns tons of data. The min and max values would have to
be very different if the data values ranged from 0-1 vs. 0-1000000.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josiah Carlson  
View profile  
 More options Feb 28, 4:23 pm
From: Josiah Carlson <josiah.carl...@gmail.com>
Date: Thu, 28 Feb 2013 13:23:31 -0800
Local: Thurs, Feb 28 2013 4:23 pm
Subject: Re: sorted set get rank by score feature suggestion
The original post that you resurrected only cared about getting a rank
of an item from the score.

In this case, we can trivially address your concern by using the
'limit' clause available to ZRANGEBYSCORE.

Also, if *you* have a particular problem you are looking to solve that
is unrelated to the original post, maybe you want to describe your
problem.

Regards,
 - Josiah


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Broglie  
View profile  
 More options Feb 28, 4:53 pm
From: Chris Broglie <cbrog...@gmail.com>
Date: Thu, 28 Feb 2013 13:53:33 -0800 (PST)
Local: Thurs, Feb 28 2013 4:53 pm
Subject: Re: sorted set get rank by score feature suggestion

I may be missing something, but I don't believe that the 'limit' clause
addresses my concern. Let me try to rephrase: given an arbitrary score (one
which may or may not be present in the set), I want to get the rank of the
closest key.

The problem with using ZRANGEBYSCORE is that without intrinsic knowledge of
the values in the set, there is no way to figure out which min and max
values to use. Let's say for example the set contained the elements
[1,5,7,8,9,10], and I wanted to get the rank for the score 6. In this case,
I could naively use +/-1 for the range and pass in 5 and 7 as the min and
max. ZRANGEBYSCORE would return 2 elements, and I could use the either one.
Great. But what if I want the rank of score 3? Passing in 2 and 4 for the
min and max would return no data.

Sure, in this example using a larger range for the min and max (say +/- 2)
from the target score would work. But this is just a toy example. What if
the set contained a million elements between 1 and 5? You could limit to
the first x results, but that's returning the first x results, rather than
the x in the middle of the range.

Does that make sense?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josiah Carlson  
View profile  
 More options Feb 28, 5:09 pm
From: Josiah Carlson <josiah.carl...@gmail.com>
Date: Thu, 28 Feb 2013 14:09:05 -0800
Local: Thurs, Feb 28 2013 5:09 pm
Subject: Re: sorted set get rank by score feature suggestion
ZRANGEBYSCORE key 6 inf limit 0 1

You can use 'inf' and '-inf' as endpoints. This particular use of
limit guarantees you are only returning 1 item.

But maybe I'm not understanding your question. Do you want the next
larger? Next smaller? Either? Both? The closest?

If you want the closest, then you can use ZRANGEBYSCORE as I
described, and also 'ZREVRANGEBYSCORE key 6 -inf limit 0 1' (which get
the next smaller if the score doesn't exist), then based on the scores
compared to the value you were actually interested in, you could then
decide which to use, what rank to pull ranges from, etc.

 - Josiah


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Broglie  
View profile  
 More options Feb 28, 5:32 pm
From: Chris Broglie <cbrog...@gmail.com>
Date: Thu, 28 Feb 2013 14:32:38 -0800 (PST)
Local: Thurs, Feb 28 2013 5:32 pm
Subject: Re: sorted set get rank by score feature suggestion

You are absolutely right. The concept that I was missing was using the
target score as one of the bounds. Sorry that it took me so long to grasp
that :)

-Chris


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josiah Carlson  
View profile  
 More options Feb 28, 6:31 pm
From: Josiah Carlson <josiah.carl...@gmail.com>
Date: Thu, 28 Feb 2013 15:31:09 -0800
Local: Thurs, Feb 28 2013 6:31 pm
Subject: Re: sorted set get rank by score feature suggestion
No worries, I'm glad your problem is now solved :)

 - Josiah


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »