123456789_123456789_123456789_123456789_123456789_

Class: Redis::Commands::Search::TextField

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 TEXT field, indexing full-text searchable content.

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) ⇒ TextField

Build a TEXT 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 #match

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :weight (Numeric)

    the field's scoring weight (+WEIGHT+)

  • :phonetic (String)

    phonetic matcher, one of dm:en, dm:fr, dm:pt, dm:es (+PHONETIC+)

  • :no_stem (Boolean)

    disable stemming (+NOSTEM+)

Raises:

  • (ArgumentError)

    if :phonetic is not a supported matcher

[ GitHub ]

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

def initialize(name, query = nil, **options)
  super(name, :text, query, **options)
  if options[:phonetic]
    valid_matchers = ['dm:en', 'dm:fr', 'dm:pt', 'dm:es']
    unless valid_matchers.include?(options[:phonetic])
      raise ArgumentError, "Invalid phonetic matcher. Supported matchers are: #{valid_matchers.join(', ')}"
    end
  end
end

Instance Method Details

#match(pattern, raw: false) ⇒ Query

Add a text-match predicate (+@field:(pattern)+) to the bound query.

Parameters:

  • pattern (String)

    the text pattern to match

  • raw (Boolean)

    when true, treat pattern as a raw RediSearch expression so operators such as wildcards (+*+) and OR (+|+) apply; when false (default) the pattern is escaped and matched literally

Returns:

  • (Query)

    the bound query with the predicate added

[ GitHub ]

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

def match(pattern, raw: false)
  query.add_predicate(TextMatchPredicate.new(@alias_name || name, pattern, raw: raw))
end

#to_argsArray

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

Returns:

  • (Array)

    the schema tokens for this field

[ GitHub ]

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

def to_args
  args = [@name]
  args << "AS" << @alias_name if @alias_name
  args << @type.to_s.upcase
  args << "NOSTEM" if @options[:no_stem]
  args << "WEIGHT" << @options[:weight].to_s if @options[:weight]
  args << "PHONETIC" << @options[:phonetic] if @options[:phonetic]

  # 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