123456789_123456789_123456789_123456789_123456789_

Class: Redis::Commands::Search::SchemaDefinition

Relationships & Source Files
Inherits: Object
Defined in: lib/redis/commands/modules/search/schema.rb

Overview

The block DSL used by Schema.build to declare fields. Each helper appends a Field subclass to #fields.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newSchemaDefinition

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/schema.rb', line 74

def initialize
  @fields = []
end

Instance Attribute Details

#fields (readonly)

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/schema.rb', line 72

attr_reader :fields

Instance Method Details

#geo_field(name, **options) ⇒ Array<Field>

Add a GeoField to the schema.

Parameters:

  • name (String, Symbol)

    the document attribute the field indexes

Returns:

  • (Array<Field>)

    the updated field list

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/schema.rb', line 136

def geo_field(name, **options)
  @fields << GeoField.new(name, **options)
end

#geoshape_field(name, coord_system = nil, **options) ⇒ Array<Field>

Add a GeoShapeField to the schema.

Parameters:

  • name (String, Symbol)

    the document attribute the field indexes

  • coord_system (String, nil) (defaults to: nil)

    the coordinate system, FLAT or SPHERICAL; nil (the default) omits the token so the server default (+SPHERICAL+) applies

Returns:

  • (Array<Field>)

    the updated field list

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/schema.rb', line 146

def geoshape_field(name, coord_system = nil, **options)
  @fields << GeoShapeField.new(name, coord_system, **options)
end

#numeric_field(name, **options) ⇒ Array<Field>

Add a NumericField to the schema.

Parameters:

  • name (String, Symbol)

    the document attribute the field indexes

Returns:

  • (Array<Field>)

    the updated field list

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/schema.rb', line 105

def numeric_field(name, **options)
  @fields << NumericField.new(name, **options)
end

#tag_field(name, **options) ⇒ Array<Field>

Add a TagField to the schema.

Parameters:

  • name (String, Symbol)

    the document attribute the field indexes

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :sortable (Boolean)

    allow sorting by the field

  • :no_index (Boolean)

    do not index the field

  • :as (String)

    an alias for the field

  • :separator (String)

    the tag separator character

  • :case_sensitive (Boolean)

    keep tag casing

  • :index_empty (Boolean)

    index empty values

  • :index_missing (Boolean)

    index documents missing the field

  • :withsuffixtrie (Boolean)

    build a suffix trie

Returns:

  • (Array<Field>)

    the updated field list

Raises:

  • (ArgumentError)

    if an unknown option key is given

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/schema.rb', line 122

def tag_field(name, **options)
  valid_options = %i[sortable no_index as separator case_sensitive index_empty index_missing withsuffixtrie]
  invalid_options = options.keys - valid_options
  if invalid_options.any?
    raise ArgumentError, "Invalid options for tag field: #{invalid_options.join(', ')}"
  end

  @fields << TagField.new(name, **options)
end

#text_field(name, **options) ⇒ Array<Field>

Add a TextField to the schema.

Parameters:

  • name (String, Symbol)

    the document attribute the field indexes

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :weight (Numeric)

    the field's scoring weight

  • :sortable (Boolean)

    allow sorting by the field

  • :no_index (Boolean)

    do not index the field

  • :as (String)

    an alias for the field

  • :phonetic (String)

    phonetic matcher

  • :no_stem (Boolean)

    disable stemming

  • :index_empty (Boolean)

    index empty values

  • :withsuffixtrie (Boolean)

    build a suffix trie

Returns:

  • (Array<Field>)

    the updated field list

Raises:

  • (ArgumentError)

    if an unknown option key is given

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/schema.rb', line 91

def text_field(name, **options)
  valid_options = %i[weight sortable no_index as phonetic no_stem index_empty index_missing withsuffixtrie]
  invalid_options = options.keys - valid_options
  if invalid_options.any?
    raise ArgumentError, "Invalid options for text field: #{invalid_options.join(', ')}"
  end

  @fields << TextField.new(name, **options)
end

#vector_field(name, algorithm, **attributes) { ... } ⇒ Array<Field>

Add a VectorField to the schema.

Field-level options (+:as+, :sortable, :no_index) are extracted from attributes; the remaining keys (e.g. type:, dim:, distance_metric:) become vector attributes. An optional block is evaluated in a VectorFieldDefinition for declaring attributes.

Examples:

vector_field "embedding", "HNSW", type: "FLOAT32", dim: 4, distance_metric: "L2"

Parameters:

  • name (String, Symbol)

    the document attribute the field indexes

  • algorithm (String, Symbol)

    the indexing method (+FLAT+, HNSW, SVS-VAMANA)

  • attributes (Hash)

    vector attributes and field-level options

Yields:

Returns:

  • (Array<Field>)

    the updated field list

[ GitHub ]

  
# File 'lib/redis/commands/modules/search/schema.rb', line 165

def vector_field(name, algorithm, **attributes, &block)
  # Extract field-level options (as, sortable, no_index) from attributes
  field_options = {}
  field_options[:as] = attributes.delete(:as) if attributes.key?(:as)
  field_options[:sortable] = attributes.delete(:sortable) if attributes.key?(:sortable)
  field_options[:no_index] = attributes.delete(:no_index) if attributes.key?(:no_index)

  field = VectorField.new(name, algorithm, attributes, **field_options)
  VectorFieldDefinition.new(field).instance_eval(&block) if block_given?
  @fields << field
end