123456789_123456789_123456789_123456789_123456789_

Class: Redis::Commands::Search::Field

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/redis/commands/modules/search/field.rb

Overview

Base class for a single field in a ::Redis Query Engine SCHEMA (the field list passed to FT.CREATE). Subclasses describe the field type (+TEXT+, TAG, NUMERIC, GEO, GEOSHAPE, VECTOR).

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

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

Build a field definition.

Examples:

Redis::Commands::Search::Field.new("title", :text, weight: 5.0, sortable: true)

Parameters:

  • name (String, Symbol)

    the document attribute the field indexes

  • type (Symbol)

    the field type, e.g. :text, :tag, :numeric

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

    a query the field is bound to, enabling predicate helpers

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :as (String)

    an alias for the field, rendered as AS <alias>

  • :no_index (Boolean)

    do not index the field (+NOINDEX+)

  • :index_missing (Boolean)

    index documents missing the field (+INDEXMISSING+)

  • :index_empty (Boolean)

    index empty values (+INDEXEMPTY+)

  • :sortable (Boolean)

    allow sorting by the field (+SORTABLE+)

  • :withsuffixtrie (Boolean)

    build a suffix trie (+WITHSUFFIXTRIE+)

[ GitHub ]

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

def initialize(name, type, query = nil, **options)
  @name = name.to_s
  @type = type
  @query = query
  @options = options
  @alias_name = options.delete(:as)
end

Instance Attribute Details

#alias_name (readonly)

[ GitHub ]

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

attr_reader :name, :type, :options, :alias_name

#name (readonly)

[ GitHub ]

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

attr_reader :name, :type, :options, :alias_name

#options (readonly)

[ GitHub ]

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

attr_reader :name, :type, :options, :alias_name

#query (rw)

[ GitHub ]

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

attr_accessor :query

#type (readonly)

[ GitHub ]

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

attr_reader :name, :type, :options, :alias_name

Instance Method Details

#to_argsArray

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

Examples:

Redis::Commands::Search::Field.new("title", :text, weight: 5.0, sortable: true).to_args
  # => ["title", "TEXT", "WEIGHT", "5.0", "SORTABLE"]

Returns:

  • (Array)

    the schema tokens for this field

[ GitHub ]

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

def to_args
  args = [@name]
  args << "AS" << @alias_name if @alias_name
  args << @type.to_s.upcase

  # Add type-specific options first (separator, casesensitive for TAG)
  if @type == :tag
    args << "SEPARATOR" << @options[:separator] if @options[:separator]
    args << "CASESENSITIVE" if @options[:case_sensitive]
  end

  # Add suffix options in specific order: no_index, index_missing, index_empty, sortable, withsuffixtrie
  args << "NOINDEX" if @options[:no_index]
  args << "INDEXMISSING" if @options[:index_missing]
  args << "INDEXEMPTY" if @options[:index_empty]
  args << "SORTABLE" if @options[:sortable]
  args << "WITHSUFFIXTRIE" if @options[:withsuffixtrie]

  args
end