Redis Sets with scores

Hi,
Is there a way to assign scores to each member of a Redis Set without convert the data structure to a sorted set?
Thanks

1 Like

Natively, I don’t think this is possible. However, when we want to perform some set operations on sorted set and set e.g (ZINTERSTORE), the weight attribute can be used as the score for the set in order to perform this operation. This in itself does not mean that the underlying set now has score associated with its values.

1 Like

Nope. That’s not what Redis Sets do. That said, depending on what you are using the set for, there might be some clever ways to store that data in other keys.

@JRedis2020

I think only best option is migrate data to Redis Sorted Set - if its inside Redis.

other option might be using RediSearch with Member id and Score value - you can do search query ordered by score, but that wont be quite efficient than sorted set.

Regards,
Suyog

Within the ordered sets we talk about scores:

These are made up of elements in unique chains that do not repeat themselves.

the elements that are in an ordered set are associated with a floating point value called punctuation.

Here is this explanation that appears in the documentation and that can be of great help to clear up questions:

If A and B are two items with a different score, then A> B if A.score is> B.score.
If A and B have exactly the same score, then A> B if string A is lexicographically greater than string B. Strings A and B cannot be equal since ordered sets only have unique elements.

Within the documentation I found a part that I thought was very helpful to share in this discussion, these are Elements with the same Punctuation:

as we know the same element cannot be repeated in an ordered set, since as we know each element is unique, it is possible to add several elements that have the same punctuation. When this occurs, the items are ordered lexicographically.

The Lexicographical order is binary, that is, it compares strings with an array of Bytes.

There is no way to assign scores in a redis set on its own. Having said that, you can always create a supplementary data structure, e.g. index.

For example, by creating another sorted set or a hash to store your scores.