123456789_123456789_123456789_123456789_123456789_

Class: Redis::Commands::Search::NumericField

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Field
Instance Chain:
self, Field
Inherits: Redis::Commands::Search::Field
Defined in: lib/redis/commands/modules/search/field.rb

Overview

A NUMERIC field, indexing numeric values for range queries.

Class Method Summary

Field - Inherited

.new

Build a field definition.

Instance Attribute Summary

Instance Method Summary

Field - Inherited

#to_args

Render this field as the array of FT.CREATE SCHEMA tokens.

Constructor Details

.new(name, query = nil, **options) ⇒ NumericField

Build a NUMERIC field.

Parameters:

  • name (String, Symbol)

    the document attribute the field indexes

  • query (Query, nil) (defaults to: nil)

    a query the field is bound to, enabling #gt, #lt, #between

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/field.rb', line 144

def initialize(name, query = nil, **options)
  super(name, :numeric, query, **options)
end

Instance Method Details

#between(min, max) ⇒ Query

Add an inclusive range predicate to the bound query.

Parameters:

  • min (Numeric)

    the inclusive lower bound

  • max (Numeric)

    the inclusive upper bound

Returns:

  • (Query)

    the bound query with the predicate added

Raises:

  • (ArgumentError)

    if min or max is not numeric

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/field.rb', line 172

def between(min, max)
  # Coerce to numeric so a value can't inject range/query syntax. RangePredicate renders
  # bounds verbatim, so the field boundary is where untrusted input must be validated.
  query.add_predicate(RangePredicate.new(@alias_name || name, Float(min), Float(max)))
end

#gt(value) ⇒ Query

Add a greater-than range predicate to the bound query.

Parameters:

  • value (Numeric)

    the exclusive lower bound

Returns:

  • (Query)

    the bound query with the predicate added

Raises:

  • (ArgumentError)

    if value is not numeric

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/field.rb', line 153

def gt(value)
  query.add_predicate(RangePredicate.new(@alias_name || name, "(#{Float(value)}", "+inf"))
end

#lt(value) ⇒ Query

Add a less-than range predicate to the bound query.

Parameters:

  • value (Numeric)

    the exclusive upper bound

Returns:

  • (Query)

    the bound query with the predicate added

Raises:

  • (ArgumentError)

    if value is not numeric

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/field.rb', line 162

def lt(value)
  query.add_predicate(RangePredicate.new(@alias_name || name, "-inf", "(#{Float(value)}"))
end