Class: Redis::Commands::Search::AggregateRequest
- Inherits:
-
Object
- Object
- Redis::Commands::Search::AggregateRequest
- Defined in:
- lib/redis/commands/modules/search/aggregation.rb
Overview
A fluent builder for FT.AGGREGATE requests.
Aggregation steps (+group_by+, sort_by, apply, filter, limit, load) are
chained on; each returns self. #to_redis_args renders them into the argument array
passed to FT.AGGREGATE, emitting LOAD steps before DIALECT and the remaining steps
after it.
Instance Method Summary collapse
-
#add_scores(add_scores: true) ⇒ self
Include the document scores in the aggregation (+ADDSCORES+).
-
#apply(expressions) ⇒ self
Add one
APPLYstep per expression. -
#dialect(dialect_version) ⇒ self
Set the query
DIALECTversion. -
#filter(expression) ⇒ self
Add a
FILTERstep that keeps rows matchingexpression. -
#group_by(fields, *reducers) ⇒ self
Add a
GROUPBYstep with optionalREDUCEfunctions. -
#initialize(query = "*", with_cursor: false, cursor_count: nil, cursor_max_idle: nil, dialect: DEFAULT_DIALECT) ⇒ AggregateRequest
constructor
A new instance of AggregateRequest.
-
#limit(offset, num) ⇒ self
Add a
LIMIT(paging) step. -
#load(*fields) ⇒ self
Add a
LOADstep. -
#sort_by(*sort_by_fields, max: nil) ⇒ self
Add a
SORTBYstep. -
#to_redis_args ⇒ Array
Render the request into the
FT.AGGREGATEargument array.
Constructor Details
#initialize(query = "*", with_cursor: false, cursor_count: nil, cursor_max_idle: nil, dialect: DEFAULT_DIALECT) ⇒ AggregateRequest
Returns a new instance of AggregateRequest.
22 23 24 25 26 27 28 29 30 |
# File 'lib/redis/commands/modules/search/aggregation.rb', line 22 def initialize(query = "*", with_cursor: false, cursor_count: nil, cursor_max_idle: nil, dialect: DEFAULT_DIALECT) @query = query @with_cursor = with_cursor @cursor_count = cursor_count @cursor_max_idle = cursor_max_idle @dialect = dialect @steps = [] end |
Instance Method Details
#add_scores(add_scores: true) ⇒ self
Include the document scores in the aggregation (+ADDSCORES+).
120 121 122 123 |
# File 'lib/redis/commands/modules/search/aggregation.rb', line 120 def add_scores(add_scores: true) @add_scores = add_scores self end |
#apply(expressions) ⇒ self
Add one APPLY step per expression.
77 78 79 80 81 82 |
# File 'lib/redis/commands/modules/search/aggregation.rb', line 77 def apply(expressions) expressions.each do |as, expression| @steps << ["APPLY", expression, "AS", as.to_s] end self end |
#dialect(dialect_version) ⇒ self
Set the query DIALECT version.
129 130 131 132 |
# File 'lib/redis/commands/modules/search/aggregation.rb', line 129 def dialect(dialect_version) @dialect = dialect_version self end |
#filter(expression) ⇒ self
Add a FILTER step that keeps rows matching expression.
98 99 100 101 |
# File 'lib/redis/commands/modules/search/aggregation.rb', line 98 def filter(expression) @steps << ["FILTER", expression] self end |
#group_by(fields, *reducers) ⇒ self
Add a GROUPBY step with optional REDUCE functions.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/redis/commands/modules/search/aggregation.rb', line 37 def group_by(fields, *reducers) step = ["GROUPBY", Array(fields).size.to_s, *Array(fields)] reducers.each do |reducer| step.concat(["REDUCE", reducer.name, reducer.args.size.to_s]) step.concat(reducer.args) step.concat(["AS", reducer.alias_name]) if reducer.alias_name end @steps << step.flatten self end |
#limit(offset, num) ⇒ self
Add a LIMIT (paging) step.
89 90 91 92 |
# File 'lib/redis/commands/modules/search/aggregation.rb', line 89 def limit(offset, num) @steps << ["LIMIT", offset, num] self end |
#load(*fields) ⇒ self
Add a LOAD step. With no fields, loads all attributes (+LOAD *+).
107 108 109 110 111 112 113 114 |
# File 'lib/redis/commands/modules/search/aggregation.rb', line 107 def load(*fields) @steps << if fields.empty? ["LOAD", "*"] else ["LOAD", fields.size, *fields.flatten] end self end |
#sort_by(*sort_by_fields, max: nil) ⇒ self
Add a SORTBY step.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/redis/commands/modules/search/aggregation.rb', line 54 def sort_by(*sort_by_fields, max: nil) # Count total arguments (field + order for each) nargs = sort_by_fields.sum do |field| field.is_a?(Asc) || field.is_a?(Desc) ? 2 : 1 end step = ["SORTBY", nargs] sort_by_fields.each do |field| if field.is_a?(Asc) || field.is_a?(Desc) step << field.name << field.order else step << field end end step << "MAX" << max if max @steps << step self end |
#to_redis_args ⇒ Array
Render the request into the FT.AGGREGATE argument array.
LOAD steps are emitted before DIALECT and all other steps after it.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/redis/commands/modules/search/aggregation.rb', line 139 def to_redis_args args = [@query] if @with_cursor args << "WITHCURSOR" args << "COUNT" << @cursor_count if @cursor_count args << "MAXIDLE" << @cursor_max_idle if @cursor_max_idle end args << "ADDSCORES" if @add_scores # Add LOAD steps first (before DIALECT) load_steps = @steps.select { |step| step[0] == "LOAD" } load_steps.each { |step| args.concat(step) } args << "DIALECT" << @dialect if @dialect # Add remaining steps (after DIALECT) other_steps = @steps.reject { |step| step[0] == "LOAD" } other_steps.each { |step| args.concat(step) } args end |