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
:vectorproperty: path,numDimensions(dimensions:),similarity(similarity:, defaultcosine), and the optional index-sidequantization:(:scalar/:binary); agent_searchable filter_fields:, the fields the agent tool lets a caller pass asvector_filter:(pointer fields map to their_p_<column>storage path, matching the pointer translation Retrieval.retrieve applies);- the
agent_tenant_scopefield, which retrieval folds into$vectorSearch.filteron 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!).
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
:vectorproperty. %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
-
.build(model_class, field: nil) ⇒ Hash
Build the definition for one
:vectorfield ofmodel_class. -
.diff(declared, live) ⇒ Hash
Structured, deterministic comparison of a declared definition with a live one.
-
.preview(model_class, name:, field: nil) ⇒ Hash
Preview the declaration a
vector_search_indexmacro would apply.
Class Method Details
.build(model_class, field: nil) ⇒ Hash
Build the definition for one :vector field of model_class.
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) = 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" => [:dimensions], "similarity" => ([:similarity] || DEFAULT_SIMILARITY).to_s, } vector["quantization"] = [:quantization].to_s if [: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).
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.
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 |