Method: Redis#zrangebyscore

Defined in:
lib/redis.rb

#zrangebyscore(key, min, max, options = {}) ⇒ Array<String>, Array<[String, Float]>

Return a range of members in a sorted set, by score.

Examples:

Retrieve members with score >= 5 and < 100

redis.zrangebyscore("zset", "5", "(100")
  # => ["a", "b"]

Retrieve the first 2 members with score >= 0

redis.zrangebyscore("zset", "0", "+inf", :limit => [0, 2])
  # => ["a", "b"]

Retrieve members and their scores with scores > 5

redis.zrangebyscore("zset", "(5", "+inf", :with_scores => true)
  # => [["a", 32.0], ["b", 64.0]]

Parameters:

  • key (String)
  • min (String)
    • inclusive minimum score is specified verbatim
    • exclusive minimum score is specified by prefixing (
  • max (String)
    • inclusive maximum score is specified verbatim
    • exclusive maximum score is specified by prefixing (
  • options (Hash) (defaults to: {})
    • :with_scores => true: include scores in output
    • :limit => [offset, count]: skip offset members, return a maximum of count members

Returns:

  • (Array<String>, Array<[String, Float]>)
    • when :with_scores is not specified, an array of members
    • when :with_scores is specified, an array with [member, score] pairs


1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# File 'lib/redis.rb', line 1001

def zrangebyscore(key, min, max, options = {})
  args = []

  with_scores = options[:with_scores] || options[:withscores]
  args.concat ["WITHSCORES"] if with_scores

  limit = options[:limit]
  args.concat ["LIMIT", *limit] if limit

  synchronize do
    @client.call [:zrangebyscore, key, min, max, *args] do |reply|
      if with_scores
        if reply
          reply.each_slice(2).map do |member, score|
            [member, Float(score)]
          end
        end
      else
        reply
      end
    end
  end
end