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

Inherits:
Object
  • Object
show all
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).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(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+)



27
28
29
30
31
32
33
# 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_nameObject (readonly)

Returns the value of attribute alias_name.



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

def alias_name
  @alias_name
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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