Class: Redis::Commands::Search::HybridPostProcessingConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/commands/modules/search/hybrid.rb

Overview

Builder for the post-processing pipeline applied to FT.HYBRID results.

Chainable setters (+load+, group_by, apply, sort_by, filter, limit) return self; #build_args renders the configured steps into argument tokens.

Instance Method Summary collapse

Constructor Details

#initializeHybridPostProcessingConfig

Returns a new instance of HybridPostProcessingConfig.



222
223
224
225
226
227
228
229
# File 'lib/redis/commands/modules/search/hybrid.rb', line 222

def initialize
  @load_statements = []
  @apply_statements = []
  @groupby_statements = []
  @sortby_fields = []
  @filter = nil
  @limit = nil
end

Instance Method Details

#apply(**kwexpr) ⇒ self

Add one APPLY step per expression.

Parameters:

  • kwexpr (Hash{Symbol => String})

    map of result alias => expression

Returns:

  • (self)


264
265
266
267
268
269
270
271
272
273
# File 'lib/redis/commands/modules/search/hybrid.rb', line 264

def apply(**kwexpr)
  apply_args = []
  kwexpr.each do |alias_name, expr|
    ret = ["APPLY", expr]
    ret.concat(["AS", alias_name.to_s]) if alias_name
    apply_args.concat(ret)
  end
  @apply_statements.concat(apply_args)
  self
end

#build_argsArray

Render the post-processing pipeline into its argument tokens.

Steps are emitted in order: LOAD, GROUPBY, APPLY, SORTBY, FILTER, LIMIT.

Returns:

  • (Array)

    the argument token array



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/redis/commands/modules/search/hybrid.rb', line 308

def build_args
  args = []
  args.concat(@load_statements) if @load_statements.any?
  args.concat(@groupby_statements) if @groupby_statements.any?
  args.concat(@apply_statements) if @apply_statements.any?
  if @sortby_fields.any?
    sortby_args = []
    @sortby_fields.each do |f|
      sortby_args.concat(f.args)
    end
    args.concat(["SORTBY", sortby_args.size, *sortby_args])
  end
  args.concat(@filter.args) if @filter
  args.concat(["LIMIT", @limit[:offset], @limit[:num]]) if @limit
  args
end

#filter(flt) ⇒ self

Add a FILTER step.

Parameters:

Returns:

  • (self)


288
289
290
291
# File 'lib/redis/commands/modules/search/hybrid.rb', line 288

def filter(flt)
  @filter = flt
  self
end

#group_by(fields, *reducers) ⇒ self

Add a GROUPBY step with optional REDUCE functions.

Parameters:

  • fields (String, Array<String>)

    the field(s) to group by

  • reducers (Array<Reducers>)

    the reducers applied to each group

Returns:

  • (self)


249
250
251
252
253
254
255
256
257
258
# File 'lib/redis/commands/modules/search/hybrid.rb', line 249

def group_by(fields, *reducers)
  ret = ["GROUPBY", Array(fields).size.to_s, *Array(fields)]
  reducers.each do |reducer|
    ret.concat(["REDUCE", reducer.name, reducer.args.size.to_s])
    ret.concat(reducer.args)
    ret.concat(["AS", reducer.alias_name]) if reducer.alias_name
  end
  @groupby_statements.concat(ret)
  self
end

#limit(offset, num) ⇒ self

Add a LIMIT (paging) step.

Parameters:

  • offset (Integer)

    the index of the first row to return

  • num (Integer)

    the maximum number of rows to return

Returns:

  • (self)


298
299
300
301
# File 'lib/redis/commands/modules/search/hybrid.rb', line 298

def limit(offset, num)
  @limit = { offset: offset, num: num }
  self
end

#load(*fields) ⇒ self

Add a LOAD step loading the given fields.

Parameters:

  • fields (Array<String>)

    the fields to load (space-separated strings are split)

Returns:

  • (self)


235
236
237
238
239
240
241
242
# File 'lib/redis/commands/modules/search/hybrid.rb', line 235

def load(*fields)
  unless fields.empty?
    fields_str = fields.join(" ")
    fields_list = fields_str.split(" ")
    @load_statements.concat(["LOAD", fields_list.size, *fields_list])
  end
  self
end

#sort_by(*sortby_fields) ⇒ self

Add a SORTBY step.

Parameters:

  • sortby_fields (Array<SortbyField>)

    the fields to sort by

Returns:

  • (self)


279
280
281
282
# File 'lib/redis/commands/modules/search/hybrid.rb', line 279

def sort_by(*sortby_fields)
  @sortby_fields = sortby_fields
  self
end