Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordered set in Redis based on more than one criteria

I am trying to create a redis ordered set. The catch is the ordering need to be done based on two criteria, timestamp and score. But in redis, I can only provide one soring criteria:

ZADD {key} {timestamp} {value}

How to add score in this ordering as well plz?

Update: Here is the approach I have taken to merge the two different ordering factoring into a single float score value:

var score = Math.floor(result.created_time/(60*60*24*1000));
score = score + (result.matches/10);

Just to explain, I have first converted the timestamp to number of days. And my 'matches' value usually be 0-10. Thus, converted to a decimal value and added as fraction weight. Which giving me highest scored entries, sorted per day. Which is exactly what I needed.

like image 613
Rana Avatar asked Sep 29 '22 00:09

Rana


1 Answers

Use decimals as scores in your Sorted Set - construct the score with the timestamp being the value on the left of the decimal point and the value on the right.

like image 119
Itamar Haber Avatar answered Oct 18 '22 13:10

Itamar Haber