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.
Class Method Summary
-
.avg(property, alias_name: nil) ⇒ Reducers
Build an
AVGreducer. -
.collect(fields:, distinct: false, sort_by: nil, limit: nil, alias_name: nil) ⇒ Reducers
Build a
COLLECTreducer, 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. -
.count(alias_name: nil) ⇒ Reducers
Build a
COUNTreducer. -
.count_distinct(property, alias_name: nil) ⇒ Reducers
Build a
COUNT_DISTINCTreducer. -
.count_distinctish(property, alias_name: nil) ⇒ Reducers
Build a
COUNT_DISTINCTISH(approximate distinct count) reducer. -
.first_value(property, alias_name: nil, sort_by: nil, sort_order: nil) ⇒ Reducers
Build a
FIRST_VALUEreducer, optionally ordered by another property. -
.max(property, alias_name: nil) ⇒ Reducers
Build a
MAXreducer. -
.min(property, alias_name: nil) ⇒ Reducers
Build a
MINreducer. - .new(name, *args, alias_name: nil) ⇒ Reducers constructor
-
.quantile(property, quantile, alias_name: nil) ⇒ Reducers
Build a
QUANTILEreducer. -
.random_sample(property, sample_size, alias_name: nil) ⇒ Reducers
Build a
RANDOM_SAMPLEreducer. -
.stddev(property, alias_name: nil) ⇒ Reducers
Build a
STDDEV(standard deviation) reducer. -
.sum(property, alias_name: nil) ⇒ Reducers
Build a
SUMreducer. -
.tolist(property, alias_name: nil) ⇒ Reducers
Build a
TOLISTreducer (collects distinct values into a list). -
.collect_fields_tokens(fields)
private
Render the
FIELDSclause tokens for .collect. -
.collect_sortby_tokens(sort_by)
private
Render the optional
SORTBYclause tokens for .collect.
Instance Attribute Summary
- #alias_name ⇒ String, ... readonly
- #args ⇒ String, ... readonly
- #name ⇒ String, ... readonly
Instance Method Summary
-
#as(alias_name) ⇒ self
Set the result alias (+AS+) for this reducer.
Constructor Details
.new(name, *args, alias_name: nil) ⇒ Reducers
# 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.
# 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.
# 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.
# 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).
# 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.
# 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.
# 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.
# 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.
.max(property, alias_name: nil) ⇒ Reducers
Build a MAX reducer.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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).
# 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_name ⇒ String, ... (readonly)
#args ⇒ String, ... (readonly)
# File 'lib/redis/commands/modules/search/aggregation.rb', line 374
attr_reader :name, :args, :alias_name
#name ⇒ String, ... (readonly)
# 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.
# File 'lib/redis/commands/modules/search/aggregation.rb', line 389
def as(alias_name) @alias_name = alias_name self end