@@ -595,8 +595,8 @@ class Redis:
595
595
lambda r : r and set (r ) or set ()
596
596
),
597
597
** string_keys_to_dict (
598
- 'ZPOPMAX ZPOPMIN ZDIFF ZRANGE ZRANGEBYSCORE ZREVRANGE '
599
- 'ZREVRANGEBYSCORE' , zset_score_pairs
598
+ 'ZPOPMAX ZPOPMIN ZINTER ZDIFF ZRANGE ZRANGEBYSCORE '
599
+ 'ZREVRANGE ZREVRANGEBYSCORE' , zset_score_pairs
600
600
),
601
601
** string_keys_to_dict ('BZPOPMIN BZPOPMAX' , \
602
602
lambda r :
@@ -2959,11 +2959,28 @@ def zincrby(self, name, amount, value):
2959
2959
"Increment the score of ``value`` in sorted set ``name`` by ``amount``"
2960
2960
return self .execute_command ('ZINCRBY' , name , amount , value )
2961
2961
2962
+ def zinter (self , keys , aggregate = None , withscores = False ):
2963
+ """
2964
+ Return the intersect of multiple sorted sets specified by ``keys``.
2965
+ With the ``aggregate`` option, it is possible to specify how the
2966
+ results of the union are aggregated. This option defaults to SUM,
2967
+ where the score of an element is summed across the inputs where it
2968
+ exists. When this option is set to either MIN or MAX, the resulting
2969
+ set will contain the minimum or maximum score of an element across
2970
+ the inputs where it exists.
2971
+ """
2972
+ return self ._zaggregate ('ZINTER' , None , keys , aggregate ,
2973
+ withscores = withscores )
2974
+
2962
2975
def zinterstore (self , dest , keys , aggregate = None ):
2963
2976
"""
2964
- Intersect multiple sorted sets specified by ``keys`` into
2965
- a new sorted set, ``dest``. Scores in the destination will be
2966
- aggregated based on the ``aggregate``, or SUM if none is provided.
2977
+ Intersect multiple sorted sets specified by ``keys`` into a new
2978
+ sorted set, ``dest``. Scores in the destination will be aggregated
2979
+ based on the ``aggregate``. This option defaults to SUM, where the
2980
+ score of an element is summed across the inputs where it exists.
2981
+ When this option is set to either MIN or MAX, the resulting set will
2982
+ contain the minimum or maximum score of an element across the inputs
2983
+ where it exists.
2967
2984
"""
2968
2985
return self ._zaggregate ('ZINTERSTORE' , dest , keys , aggregate )
2969
2986
@@ -3253,8 +3270,12 @@ def zunionstore(self, dest, keys, aggregate=None):
3253
3270
"""
3254
3271
return self ._zaggregate ('ZUNIONSTORE' , dest , keys , aggregate )
3255
3272
3256
- def _zaggregate (self , command , dest , keys , aggregate = None ):
3257
- pieces = [command , dest , len (keys )]
3273
+ def _zaggregate (self , command , dest , keys , aggregate = None ,
3274
+ ** options ):
3275
+ pieces = [command ]
3276
+ if dest is not None :
3277
+ pieces .append (dest )
3278
+ pieces .append (len (keys ))
3258
3279
if isinstance (keys , dict ):
3259
3280
keys , weights = keys .keys (), keys .values ()
3260
3281
else :
@@ -3264,9 +3285,14 @@ def _zaggregate(self, command, dest, keys, aggregate=None):
3264
3285
pieces .append (b'WEIGHTS' )
3265
3286
pieces .extend (weights )
3266
3287
if aggregate :
3267
- pieces .append (b'AGGREGATE' )
3268
- pieces .append (aggregate )
3269
- return self .execute_command (* pieces )
3288
+ if aggregate .upper () in ['SUM' , 'MIN' , 'MAX' ]:
3289
+ pieces .append (b'AGGREGATE' )
3290
+ pieces .append (aggregate )
3291
+ else :
3292
+ raise DataError ("aggregate can be sum, min or max." )
3293
+ if options .get ('withscores' , False ):
3294
+ pieces .append (b'WITHSCORES' )
3295
+ return self .execute_command (* pieces , ** options )
3270
3296
3271
3297
# HYPERLOGLOG COMMANDS
3272
3298
def pfadd (self , name , * values ):
0 commit comments