Skip to content

Get Values By Score Range

Description

This operation returns values for a range of scores.

Method

Get range of values without scores

Backendless.Hive(hiveName).SortedSetStore(keyName).getRangeByScore(options, offset, pageSize, reverse);

Get range of values with scores

Backendless.Hive(hiveName).SortedSetStore(keyName).getRangeWithScoresByScore(options, offset, pageSize, reverse);

where:

Argument                Description
hiveName Name of a hive where the operation is performed. String value.
keyName Key name identifying a sorted-set. String value.
offset Sets a zero-based index/position from which to retrieve the items. For example, if the resulting set contains 10 items, then by setting this parameter to 2, the first 2 values in the range will be skipped, while the remaining 8 returned in the response.
pageSize  If not set, defaults to 20. Sets the number of items to include in the response. Maximum supported value is 10000.
reverse When this parameter is not present, it defaults to false. When set to false, items in the specified sorted set are sorted in ascending order of their scores.  When set to true, items are sorted in descending order of their scores.
options Is the ScoreRangeOptions class, consisting of parameters that control the behavior of this operation. See the description below.

ScoreRangeOptions is a class with the structure provided below:

public static final class ScoreRangeOptions
  {
    private double minScore;
    private ValueBound minBound;
    private double maxScore;
    private ValueBound maxBound;

    public ScoreRangeOptions setMinScore( double minScore )
    {
      this.minScore = minScore;
      return this;
    }

    public ScoreRangeOptions setMinBound( ValueBound minBound )
    {
      this.minBound = minBound;
      return this;
    }

    public ScoreRangeOptions setMaxScore( double maxScore )
    {
      this.maxScore = maxScore;
      return this;
    }

    public ScoreRangeOptions setMaxBound( ValueBound maxBound )
    {
      this.maxBound = maxBound;
      return this;
    }
  }

where:

Argument                Description
minScoreV/maxScore Score values between which the items are requested. To control whether the specified values are included or excluded from the range, use the minBound/maxBound parameters.
minBound/maxBound  Available options are:
ValueBound.Include - Includes value specified in the corresponding minScore/maxScore parameter into the range.
ValueBound.Exclude - Excludes value specified in the corresponding minScore/maxScore parameter from the range.
ValueBound.Infinity - Ignores the corresponding minScore/maxScore parameter and includes all consecutive score values that come before/after the defined score range. For example, if minBound is set to Infinity and maxScore is set to 5, the query will include all values with scores less than 5.

Return Value

When the getRangeWithScoresByScore method is used, the return value is an array of score-value pairs. Each pair is also an array of two elements - the score and the value. When the withScores parameter is set to false, the return value is  array of values.When the getRangeByScore is used, the return value is an array of values.

Example

The example below returns all values in the range of scores between 0 and 3.

Consider the following sorted set data:

sample-sorted-set

The example below returns values in the range of scores between 0 (inclusively) and to 3 (exclusively).

  • Since the reverse parameter is set to true, the items are sorted by rank in descending order.
  • The pageSize parameter is set to 3, indicating that the service should return 3 items in the response.
  • The offset parameter is set to 2, the returned items are included from the second (zero-based) position.

Get range of values with scores

HiveSortedSet.ScoreRangeOptions options = new HiveSortedSet.ScoreRangeOptions();
options.setMinScore( 0 );
options.setMaxScore( 3.00 );
options.setMinBound( ValueBound.Include );
options.setMaxBound( ValueBound.Exclude );

Backendless.Hive("leaderboard").SortedSetStore("players").getRangeWithScoresByScore(options, 2, 3, true);

where:

Argument                Description
"leaderboard" Name of a hive in the system.
"players" Name of a sorted set where the operation takes place.

Response

[  
  [  
    2.45,  
    "Ken"  
  ],  
  [  
    2.3,  
    "Frank"  
  ],  
  [  
    2.25,  
    "Ana"  
  ]  
]

If the getRangeByScore method is used, the response does not include the scores:

Get range of values without scores

HiveSortedSet.ScoreRangeOptions options = new HiveSortedSet.ScoreRangeOptions();
options.setMinScore( 0 );
options.setMaxScore( 3.00 );
options.setMinBound( ValueBound.Include );
options.setMaxBound( ValueBound.Exclude );

Backendless.Hive("leaderboard").SortedSetStore("players").getRangeByScore(options, 2, 3, true);
[  
  "Ken",  
  "Frank",  
  "Ana"  
]

Codeless Reference
sorted_set_api_get_values_by_score_range

where:

Argument                Description
hive name Name of a hive where the operation is performed.
key name Key name identifying a sorted set.
min/max score Score values between which the items are requested. To control whether the specified values are included or excluded from the range, use the min/max bound parameters.
min/max bound Parameters that control the presence of the first and last values in the range. Each of these parameters has three options:

Include - Includes value specified in the corresponding min score/max score parameter into the range.

Exclude - Excludes value specified in the corresponding min score/max score parameter from the range.

Infinity - Ignores the corresponding min score/max score parameter and includes all consecutive score values that come before/after the defined score range. For example, if min bound is set to Infinity and max score is set to 5, the query will include all values with scores less than 5.

These parameters default to Include.
offset Sets a zero-based index/position from which to retrieve the items. For example, if the resulting set contains 10 items, then by setting the offset parameter to 2, the first 2 values in the range will be skipped, while the remaining 8 returned in the response.
page size If not set, defaults to 20. Sets the number of items to include in the response. Maximum supported value is 10000.
with score When set to true, then the scores are returned along with the values. Otherwise, when set to false, the scores are not returned. When the parameter is not present, it defaults to false.
reverse When set to true, items are sorted in ascending order. If set to false, items are sorted in descending order.

When the with score parameter is set to true, the return value is a list of score-value pairs. Each pair is also a list of two elements - the score and the value. When the with scores parameter is set to false, the return value is a list of values.

Consider the following Sorted Set storage:
sorted_set_api_example_all_blocks

The example below returns values in the range of scores between 0 (inclusively) and to 5 (inclusively).

  • Since the with score parameter is set to true, the keys are returned alongside with the corresponding scores.
  • Since the reverse parameter is set to true, the items are sorted by rank in descending order.
  • The page size parameter is set to 3, indicating that the service should return 3 items in the response.
  • The offset parameter is set to 1, the returned items are included from the first (zero-based) position.


sorted_set_api_example_get_values_by_score_range


The output will look as shown below after the Codeless logic runs:
sorted_set_api_example_get_values_by_score_range_2