How can Redis sort according to two different sorted sets?

筅森魡賤 提交于 2020-01-06 11:10:21

问题


I have two different sorted sets.

One is for editor ID:

article_id  editor_id
101         10
102         11
103         10
104         10

The other sorted set is for date sorting:

article_id  day
101         29
102         27
103         25
104         27

I want to merge these sets which shows first editor second day sorted state. Which commands should I use?


回答1:


Assuming that article_id is your members' value and that editor_id/day are the scores in the respective Sorted Set, and assuming each article_id is present in both Sorted Sets, you can do the following:

ZINTERSTORE t 2 k1 k2 WEIGHTS 100 1 AGGREGATE SUM

Explanation:

  • t is a temporary key that will hold the result
  • k1 is the Sorted Set that stores the editor_id
  • k2 is the Sorted Set that stores the day
  • the weight 100 multiplies editor_id by 100 (i.e. "shifts" it two places to the right)
  • the AGGREGATE SUM results in the following score: editor_id * 100 + day

Notes:

  • you can use ZUNIONSTORE instead for the same result
  • the use of weight 100 assumes that day is a 2-digit value


来源:https://stackoverflow.com/questions/29281920/how-can-redis-sort-according-to-two-different-sorted-sets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!