Skip to main content

ZUNION

Syntax

ZUNION key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE <SUM | MIN | MAX>] [WITHSCORES]

Module

sortedset

Categories

read slow sortedset

Description

Return the union of the sorted sets in keys. The scores of each member of a sorted set are multiplied by the corresponding weight in WEIGHTS. Aggregate determines how the scores are combined. WITHSCORES option determines whether to return the result with scores included.

Options

  • WEIGHTS - A list of floats that determine the weight of each sorted set. The scores of each member of a sort set are multiplied by the corresponding weight. If weights are not provided, the default weight is 1 for all sorted sets.
  • AGGREGATE - Determines the strategy used to compare the scores of members in the union. SUM will add the scores, MIN will choose the minimum score, and MAX will choose the maximum score.
  • WITHSCORES - Determines whether scores should be included in the resulting sorted set.

Examples

Find the union between 2 sorted sets:

db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
sortedSet, err := db.ZUnion([]string{"key1", "key2"}, db.ZUnionOptions{})

Find the union between 2 sorted sets with a sum of the weighted scores:

db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
sortedSet, err := db.ZUnion(
[]string{"key1", "key2"},
db.ZUnionOptions{Weights: []float64{2, 4}, Aggregate: "SUM", WithScores: true},
)