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

Inherits:
Field
  • Object
show all
Defined in:
lib/redis/commands/modules/search/field.rb

Overview

A VECTOR field, indexing embeddings for approximate/exact nearest-neighbor search.

Instance Attribute Summary collapse

Attributes inherited from Field

#alias_name, #name, #options, #query, #type

Instance Method Summary collapse

Constructor Details

#initialize(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:

  • (ArgumentError)

    if algorithm is not a supported indexing method

  • (Redis::CommandError)

    if :sortable or :no_index is given



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# 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

#algorithmObject (readonly)

Returns the value of attribute algorithm.



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

def algorithm
  @algorithm
end

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

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



269
270
271
272
273
# 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)



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

def args
  field_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



278
279
280
281
282
283
# 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