有序集合操作 - 评分的聚合

优质
小牛编辑
130浏览
2023-12-01
  1. ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]

例如:

  1. 127.0.0.1:6379> zrangebyscore votes -inf inf withscores
  2. 1) "sina"
  3. 2) "1"
  4. 3) "google"
  5. 4) "5"
  6. 5) "baidu"
  7. 6) "10"
  8. 127.0.0.1:6379> zrangebyscore visits -inf inf withscores
  9. 1) "baidu"
  10. 2) "1"
  11. 3) "google"
  12. 4) "5"
  13. 5) "sina"
  14. 6) "10"
  15. 127.0.0.1:6379> zunionstore award 2 visits votes weights 1 2 aggregate sum
  16. (integer) 3
  17. 127.0.0.1:6379> zrangebyscore award -inf inf withscores
  18. 1) "sina"
  19. 2) "12"
  20. 3) "google"
  21. 4) "15"
  22. 5) "baidu"
  23. 6) "21"

一个小技巧是如果需要对评分进行倍加,则使用如下的方法:

  1. 127.0.0.1:6379>zrangebyscore visits -inf inf withscores
  2. 1) "baidu"
  3. 2) "1"
  4. 3) "google"
  5. 4) "5"
  6. 5) "sina"
  7. 6) "10"
  8. 127.0.0.1:6379>zunionstore visits 1 visits weights 2
  9. (integer) 3
  10. 127.0.0.1:6379>zrangebyscore visits -inf inf withscores
  11. 1) "baidu"
  12. 2) "2"
  13. 3) "google"
  14. 4) "10"
  15. 5) "sina"
  16. 6) "20"