123456789_123456789_123456789_123456789_123456789_

Class: Redis::Commands::Search::VectorField

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 VECTOR field, indexing embeddings for approximate/exact nearest-neighbor search.

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, algorithm, attributes = {}, **options) ⇒ VectorField

Build a VECTOR field.

Examples:

Redis::Commands::Search::Field::VectorField.new(
  "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, one of FLAT, HNSW, SVS-VAMANA

  • attributes (Hash) (defaults to: {})

    the vector attributes (e.g. type, dim, distance_metric)

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :as (String)

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

Raises:

[ GitHub ]

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

def initialize(name, algorithm, attributes = {}, **options)
  # Validate algorithm
  unless ['FLAT', 'HNSW', 'SVS-VAMANA'].include?(algorithm.to_s.upcase)
    raise ArgumentError,
          "Realtime vector indexing supporting 3 Indexing Methods: 'FLAT', 'HNSW', and 'SVS-VAMANA'"
  end

  # Validate that sortable and no_index are not used with vector fields
  if options[:sortable]
    raise Redis::CommandError, "Vector fields cannot be sortable"
  end
  if options[:no_index]
    raise Redis::CommandError, "Vector fields cannot have no_index option"
  end

  super(name, :vector, **options)
  @algorithm = algorithm.to_s.upcase
  @attributes = attributes.transform_keys { |k| k.to_s.upcase }.transform_values { |v| v.to_s.upcase }
end

Instance Attribute Details

#algorithm (readonly)

[ GitHub ]

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

attr_reader :algorithm, :attributes

#attributes (readonly)

[ GitHub ]

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

attr_reader :algorithm, :attributes

Instance Method Details

#add_attribute(key, value) ⇒ Object

Set or override a single vector attribute.

Parameters:

  • key (String, Symbol)

    the attribute name (upcased internally)

  • value (Object)

    the attribute value

Returns:

  • (Object)

    the stored value

[ GitHub ]

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

def add_attribute(key, value)
  # Normalize like #initialize does for the kwargs form, so block-DSL attributes
  # (vector_field(...) { type "float32" }) reach FT.CREATE with the expected casing.
  @attributes[key.to_s.upcase] = value.to_s.upcase
end

#argsArray

Returns field-specific args (without name/alias) for compatibility with tests

Returns:

  • (Array)

    the VECTOR clause tokens (without name/alias)

[ GitHub ]

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

def args
  field_args
end

#field_args (private)

[ GitHub ]

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

def field_args
  args = ['VECTOR']
  args << @algorithm
  args << @attributes.size * 2
  @attributes.each do |k, v|
    args << k << v
  end
  args
end

#to_argsArray

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

Returns:

  • (Array)

    the schema tokens for this field, including the VECTOR clause

[ GitHub ]

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

def to_args
  args = [name]
  args << "AS" << @alias_name if @alias_name
  args += field_args
  args
end