Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

sortedset - How can Redis sort according to two different sorted sets?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...