123456789_123456789_123456789_123456789_123456789_

Class: Redis::Commands::Search::Reducers

Relationships & Source Files
Inherits: Object
Defined in: lib/redis/commands/modules/search/aggregation.rb

Overview

Factory for the REDUCE functions used inside AggregateRequest#group_by.

Each class method returns a Reducers instance carrying the function name, its arguments, and an optional result alias.

Examples:

Redis::Commands::Search::Reducers.sum("@price", alias_name: "total")

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name, *args, alias_name: nil) ⇒ Reducers

Parameters:

  • name (String)

    the reducer function name

  • args (Array)

    the reducer arguments

  • alias_name (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 379

def initialize(name, *args, alias_name: nil)
  @name = name
  @args = args
  @alias_name = alias_name
end

Class Method Details

.avg(property, alias_name: nil) ⇒ Reducers

Build an AVG reducer.

Parameters:

  • property (String)

    the property to average

  • alias_name (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 252

def self.avg(property, alias_name: nil)
  new("AVG", property, alias_name: alias_name)
end

.collect(fields:, distinct: false, sort_by: nil, limit: nil, alias_name: nil) ⇒ Reducers

Build a COLLECT reducer, which gathers each group's rows, projects a chosen set of fields, optionally deduplicates, sorts and limits them, and emits them as an array of per-entry maps under the reducer alias.

Field and sort-key names must carry an @ prefix on the wire (like the other reducers, the caller supplies it); output map keys are the same names with the @ stripped.

Examples:

top 5 per group by rating

Reducers.collect(
  fields: ["@title", "@rating"],
  sort_by: [Desc.new("@rating")],
  limit: [0, 5],
  alias_name: "top"
)

Parameters:

  • fields (Symbol, Array<String>)

    :all emits FIELDS * (projects the fields the pipeline has materialized at this stage — not a whole-document fetch); an Array emits FIELDS <count> <@field> ...

  • distinct (Boolean)

    emit DISTINCT to deduplicate entries. NOTE: not yet implemented server-side; sending it currently fails parsing. Kept for forward compatibility — do not rely on it until the server ships it.

  • sort_by (Array<String, Asc, Desc>, nil)

    sort keys; a plain String sorts ascending (the default), an Asc+/+Desc wrapper sets the direction explicitly. NOTE: SORTBY without limit returns at most 10 entries per group (the server default).

  • limit (Array(Integer, Integer), nil)

    a [offset, count] pair emitting LIMIT <offset> <count>; with sort_by this is a bounded top-N selection

  • alias_name (String, nil)

    the reducer output column name (+AS+)

Raises:

  • (ArgumentError)

    if fields is an empty list and not :all

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 339

def self.collect(fields:, distinct: false, sort_by: nil, limit: nil, alias_name: nil)
  tokens = collect_fields_tokens(fields)
  tokens << "DISTINCT" if distinct
  tokens.concat(collect_sortby_tokens(sort_by))
  tokens.concat(["LIMIT", *limit]) if limit

  new("COLLECT", *tokens, alias_name: alias_name)
end

.collect_fields_tokens(fields) (private)

Render the FIELDS clause tokens for .collect.

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 349

def self.collect_fields_tokens(fields)
  return ["FIELDS", "*"] if fields == :all

  fields = Array(fields)
  raise ArgumentError, "collect fields must be :all or a non-empty list" if fields.empty?

  ["FIELDS", fields.size.to_s, *fields]
end

.collect_sortby_tokens(sort_by) (private)

Render the optional SORTBY clause tokens for .collect. Each Asc/Desc wrapper emits a name and its direction (2 tokens); a plain String emits just the name (ASC is implicit).

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 361

def self.collect_sortby_tokens(sort_by)
  return [] if sort_by.nil? || sort_by.empty?

  sort_tokens = sort_by.flat_map do |field|
    field.is_a?(Asc) || field.is_a?(Desc) ? [field.name, field.order] : [field]
  end
  ["SORTBY", sort_tokens.size.to_s, *sort_tokens]
end

.count(alias_name: nil) ⇒ Reducers

Build a COUNT reducer.

Parameters:

  • alias_name (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 198

def self.count(alias_name: nil)
  new("COUNT", alias_name: alias_name)
end

.count_distinct(property, alias_name: nil) ⇒ Reducers

Build a COUNT_DISTINCT reducer.

Parameters:

  • property (String)

    the property to count distinct values of

  • alias_name (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 207

def self.count_distinct(property, alias_name: nil)
  new("COUNT_DISTINCT", property, alias_name: alias_name)
end

.count_distinctish(property, alias_name: nil) ⇒ Reducers

Build a COUNT_DISTINCTISH (approximate distinct count) reducer.

Parameters:

  • property (String)

    the property to count distinct values of

  • alias_name (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 216

def self.count_distinctish(property, alias_name: nil)
  new("COUNT_DISTINCTISH", property, alias_name: alias_name)
end

.first_value(property, alias_name: nil, sort_by: nil, sort_order: nil) ⇒ Reducers

Build a FIRST_VALUE reducer, optionally ordered by another property.

Parameters:

  • property (String)

    the property whose first value is taken

  • alias_name (String, nil)

    the result alias

  • sort_by (String, nil)

    the property to order by before taking the first value

  • sort_order (Symbol, String, nil)

    the sort direction ("ASC"/"DESC", default "ASC")

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 291

def self.first_value(property, alias_name: nil, sort_by: nil, sort_order: nil)
  args = [property]
  if sort_by
    args << "BY" << sort_by
    args << (sort_order || "ASC").to_s.upcase
  end
  new("FIRST_VALUE", *args, alias_name: alias_name)
end

.max(property, alias_name: nil) ⇒ Reducers

Build a MAX reducer.

Parameters:

  • property (String)

    the property to take the maximum of

  • alias_name (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 243

def self.max(property, alias_name: nil)
  new("MAX", property, alias_name: alias_name)
end

.min(property, alias_name: nil) ⇒ Reducers

Build a MIN reducer.

Parameters:

  • property (String)

    the property to take the minimum of

  • alias_name (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 234

def self.min(property, alias_name: nil)
  new("MIN", property, alias_name: alias_name)
end

.quantile(property, quantile, alias_name: nil) ⇒ Reducers

Build a QUANTILE reducer.

Parameters:

  • property (String)

    the property to compute the quantile of

  • quantile (Float)

    the quantile in the range 0..1

  • alias_name (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 271

def self.quantile(property, quantile, alias_name: nil)
  new("QUANTILE", property, quantile, alias_name: alias_name)
end

.random_sample(property, sample_size, alias_name: nil) ⇒ Reducers

Build a RANDOM_SAMPLE reducer.

Parameters:

  • property (String)

    the property to sample

  • sample_size (Integer)

    the number of values to sample

  • alias_name (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 306

def self.random_sample(property, sample_size, alias_name: nil)
  new("RANDOM_SAMPLE", property, sample_size, alias_name: alias_name)
end

.stddev(property, alias_name: nil) ⇒ Reducers

Build a STDDEV (standard deviation) reducer.

Parameters:

  • property (String)

    the property to compute the standard deviation of

  • alias_name (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 261

def self.stddev(property, alias_name: nil)
  new("STDDEV", property, alias_name: alias_name)
end

.sum(property, alias_name: nil) ⇒ Reducers

Build a SUM reducer.

Parameters:

  • property (String)

    the property to sum

  • alias_name (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 225

def self.sum(property, alias_name: nil)
  new("SUM", property, alias_name: alias_name)
end

.tolist(property, alias_name: nil) ⇒ Reducers

Build a TOLIST reducer (collects distinct values into a list).

Parameters:

  • property (String)

    the property to collect

  • alias_name (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 280

def self.tolist(property, alias_name: nil)
  new("TOLIST", property, alias_name: alias_name)
end

Instance Attribute Details

#alias_nameString, ... (readonly)

Returns:

  • (String)

    the reducer function name

  • (Array)

    the reducer arguments

  • (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 374

attr_reader :name, :args, :alias_name

#argsString, ... (readonly)

Returns:

  • (String)

    the reducer function name

  • (Array)

    the reducer arguments

  • (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 374

attr_reader :name, :args, :alias_name

#nameString, ... (readonly)

Returns:

  • (String)

    the reducer function name

  • (Array)

    the reducer arguments

  • (String, nil)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 374

attr_reader :name, :args, :alias_name

Instance Method Details

#as(alias_name) ⇒ self

Set the result alias (+AS+) for this reducer.

Parameters:

  • alias_name (String)

    the result alias

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/aggregation.rb', line 389

def as(alias_name)
  @alias_name = alias_name
  self
end