Module: Parse::VectorSearch::IndexDefinition

Defined in:
lib/parse/vector_search/index_definition.rb

Overview

Derives an Atlas vectorSearch index definition from a model's declarations, so the index an operator deploys matches what the SDK will query instead of being written by hand.

Sources, all read from the model:

  • the :vector property: path, numDimensions (dimensions:), similarity (similarity:, default cosine), and the optional index-side quantization: (:scalar / :binary);
  • agent_searchable filter_fields:, the fields the agent tool lets a caller pass as vector_filter: (pointer fields map to their _p_<column> storage path, matching the pointer translation Retrieval.retrieve applies);
  • the agent_tenant_scope field, which retrieval folds into $vectorSearch.filter on every scoped query.

Output is deterministic: the vector entry first, then filter entries sorted by path, keys in a fixed order. Generating is side-effect free; applying an index stays explicit through Schema::SearchIndexMigrator (declare it with the vector_search_index model macro and run apply_search_indexes!).

Examples:

class Article < Parse::Object
  property :embedding, :vector, dimensions: 1024, similarity: :dotProduct,
                                quantization: :scalar
  agent_searchable field: :embedding, filter_fields: %i[category]
  vector_search_index "article_vec"   # generated; applied explicitly
end

Parse::VectorSearch::IndexDefinition.build(Article)
# => { "fields" => [
#      { "type" => "vector", "path" => "embedding", "numDimensions" => 1024,
#        "similarity" => "dotProduct", "quantization" => "scalar" },
#      { "type" => "filter", "path" => "category" } ] }

Constant Summary collapse

DEFAULT_SIMILARITY =

Similarity emitted when the property declares none. Atlas requires one; cosine is the safe choice for unit-normalized embeddings.

"cosine"
QUANTIZATIONS =

Index-side quantization values accepted on a :vector property.

%w[scalar binary].freeze
VECTOR_KEY_ORDER =

Key order for each field entry, so serialized output is stable.

%w[type path numDimensions similarity quantization].freeze

Class Method Summary collapse

Class Method Details

.build(model_class, field: nil) ⇒ Hash

Build the definition for one :vector field of model_class.

Parameters:

  • model_class (Class) —

    a Parse::Object subclass.

  • field (Symbol, String, nil) (defaults to: nil) —

    the :vector property; may be omitted when the class declares exactly one searchable vector.

Returns:

  • (Hash) —

    a string-keyed { "fields" => [...] } definition.

Raises:

  • (ArgumentError) —

    when the field cannot be resolved.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/parse/vector_search/index_definition.rb', line 61

def build(model_class, field: nil)
  field_sym = resolve_field!(model_class, field)
  meta = model_class.vector_properties.fetch(field_sym)

  vector = {
    "type" => "vector",
    # The stored column (field_map), which is what $vectorSearch
    # queries: `body_embedding` is saved as `bodyEmbedding`.
    "path" => (model_class.respond_to?(:vector_storage_path) ? model_class.vector_storage_path(field_sym) : field_sym.to_s),
    "numDimensions" => meta[:dimensions],
    "similarity" => (meta[:similarity] || DEFAULT_SIMILARITY).to_s,
  }
  vector["quantization"] = meta[:quantization].to_s if meta[:quantization]

  filters = filter_paths(model_class).map { |path| { "type" => "filter", "path" => path } }
  { "fields" => [order_keys(vector)] + filters }
end

.diff(declared, live) ⇒ Hash

Structured, deterministic comparison of a declared definition with a live one. Accepts either definitions or index documents carrying latestDefinition (the shape $listSearchIndexes returns).

Parameters:

  • declared (Hash) —

    the generated (or declared) definition.

  • live (Hash, nil) —

    the live definition or index document.

Returns:

  • (Hash) —

    { in_sync:, vector: { key => { declared:, live: } }, filters_missing: [...], filters_extra: [...] }. vector lists only differing keys; filters_missing are declared filter paths absent from the live index (scoped queries on them fail Atlas-side), filters_extra are live paths not declared.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/parse/vector_search/index_definition.rb', line 97

def diff(declared, live)
  declared_defn = definition_of(declared)
  live_defn = definition_of(live)

  d_vec = vector_entry(declared_defn)
  # Compare against the live vector entry for the SAME path, so an
  # index with several vector fields does not report false drift.
  l_vec = vector_entry(live_defn, path: d_vec["path"])
  vector_changes = {}
  (VECTOR_KEY_ORDER - %w[type]).each do |key|
    dv = normalize_vector_value(key, d_vec[key])
    lv = normalize_vector_value(key, l_vec[key])
    vector_changes[key] = { declared: dv, live: lv } unless dv == lv
  end

  d_filters = filter_paths_of(declared_defn)
  l_filters = filter_paths_of(live_defn)
  missing = (d_filters - l_filters).sort
  extra = (l_filters - d_filters).sort

  {
    in_sync: vector_changes.empty? && missing.empty? && extra.empty?,
    vector: vector_changes,
    filters_missing: missing,
    filters_extra: extra,
  }
end

.preview(model_class, name:, field: nil) ⇒ Hash

Preview the declaration a vector_search_index macro would apply.

Returns:

  • (Hash) —

    { name:, type: "vectorSearch", definition: }.



82
83
84
# File 'lib/parse/vector_search/index_definition.rb', line 82

def preview(model_class, name:, field: nil)
  { name: name.to_s, type: "vectorSearch", definition: build(model_class, field: field) }
end