Module: Parse::Core::SearchIndexing
- Included in:
- Object
- Defined in:
- lib/parse/model/core/search_indexing.rb
Overview
Model-declarative Atlas Search index DSL. Mixed into Parse::Object
so subclasses can declare the Atlas Search indexes they expect to
exist on their collection. Declarations are inert at load time —
they only land on Atlas when Schema::SearchIndexMigrator
reads them and apply_search_indexes! is invoked through the
writer connection.
Parallels Indexing (the regular mongo_index DSL)
but with three meaningful differences:
- Multi-per-class. A single model can declare several search indexes (one for full-text, one for autocomplete, one for vector search), each with a unique name.
- Definition is opaque. The DSL doesn't introspect field references — Atlas owns the mapping schema. The DSL validates name shape and Hash-non-emptiness only; everything else is forwarded to Atlas verbatim.
- Async build. Mutations don't return a "READY" guarantee
— the migrator's rake task is fire-and-forget by default,
WAIT=trueopts into polling via AtlasSearch::IndexManager.wait_for_ready.
SECURITY POSTURE — purely declarative. No network I/O at
declaration time, no class introspection. The validation rules
below surface typos as load-time errors instead of runtime
surprises during rake parse:mongo:search_indexes:apply in prod.
Constant Summary collapse
- INDEX_NAME_PATTERN =
Atlas Search index name shape. Same regex used at the MongoDB.create_search_index layer.
/\A[A-Za-z][A-Za-z0-9_-]{0,63}\z/.freeze
- ALLOWED_INDEX_TYPES =
Allowed
type:values formongo_search_index.searchis the default and covers the conventional text-search / autocomplete / faceted-search use cases;vectorSearchis for vector similarity indexes. Atlas rejects any other value at command time, but we check at declaration time so the typo doesn't survive to prod. %w[search vectorSearch].freeze
Instance Method Summary collapse
-
#apply_search_indexes!(update: false, drop: false, wait: false, timeout: 600) ⇒ Hash
Apply declared search-index changes via the writer connection.
-
#generated_vector_search_index_declarations ⇒ Array<Hash>
Materialize #generated_vector_search_indexes into migrator declarations (
{ name:, definition:, type: "vectorSearch" }), generating each definition from the current model declarations. -
#generated_vector_search_indexes ⇒ Array<Hash>
{ name:, field: }entries registered by #vector_search_index. -
#mongo_search_index(name, definition, type: "search") ⇒ Hash
Declare an Atlas Search index for this model's collection.
-
#mongo_search_index_declarations ⇒ Array<Hash>
Storage for declared search indexes.
-
#search_indexes_plan ⇒ Hash
Dry-run reconciliation between declared search indexes and what exists on Atlas.
-
#vector_search_index(name, field: nil) ⇒ Hash
Declare a vectorSearch index whose definition is GENERATED from the model's declarations by VectorSearch::IndexDefinition.build (vector path, dimensions, similarity, quantization, agent_searchable filter fields, agent_tenant_scope field), instead of written by hand.
Instance Method Details
#apply_search_indexes!(update: false, drop: false, wait: false, timeout: 600) ⇒ Hash
Apply declared search-index changes via the writer connection.
211 212 213 214 215 |
# File 'lib/parse/model/core/search_indexing.rb', line 211 def apply_search_indexes!(update: false, drop: false, wait: false, timeout: 600) Parse::Schema::SearchIndexMigrator.new(self).apply!( update: update, drop: drop, wait: wait, timeout: timeout, ) end |
#generated_vector_search_index_declarations ⇒ Array<Hash>
Materialize #generated_vector_search_indexes into migrator
declarations ({ name:, definition:, type: "vectorSearch" }),
generating each definition from the current model declarations.
179 180 181 182 183 184 185 186 187 |
# File 'lib/parse/model/core/search_indexing.rb', line 179 def generated_vector_search_index_declarations generated_vector_search_indexes.map do |entry| { name: entry[:name], definition: Parse::VectorSearch::IndexDefinition.build(self, field: entry[:field]), type: "vectorSearch", }.freeze end end |
#generated_vector_search_indexes ⇒ Array<Hash>
Returns { name:, field: } entries registered by
#vector_search_index.
171 172 173 |
# File 'lib/parse/model/core/search_indexing.rb', line 171 def generated_vector_search_indexes @generated_vector_search_indexes ||= [] end |
#mongo_search_index(name, definition, type: "search") ⇒ Hash
Declare an Atlas Search index for this model's collection.
85 86 87 88 89 90 91 92 93 94 95 96 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 124 125 126 127 128 129 130 |
# File 'lib/parse/model/core/search_indexing.rb', line 85 def mongo_search_index(name, definition, type: "search") name_str = name.to_s unless name_str.match?(INDEX_NAME_PATTERN) raise ArgumentError, "#{self}.mongo_search_index name #{name.inspect} must match #{INDEX_NAME_PATTERN.inspect}" end unless definition.is_a?(Hash) && !definition.empty? raise ArgumentError, "#{self}.mongo_search_index #{name_str.inspect} requires a non-empty Hash definition; got #{definition.inspect}" end type_str = type.to_s unless ALLOWED_INDEX_TYPES.include?(type_str) raise ArgumentError, "#{self}.mongo_search_index #{name_str.inspect} type=#{type.inspect} must be one of #{ALLOWED_INDEX_TYPES.inspect}" end declaration = { name: name_str, definition: deep_freeze(definition), type: type_str, }.freeze if generated_vector_search_indexes.any? { |e| e[:name] == name_str } raise ArgumentError, "#{self}.mongo_search_index #{name_str.inspect} is already declared by " \ "vector_search_index. Each name may have one declaration per class." end existing = mongo_search_index_declarations.find { |d| d[:name] == name_str } if existing # Idempotent redeclaration with identical content — common in # autoloading / class-reopening setups. Re-declarations that # disagree on definition or type fail loudly so the operator # notices the conflict at class load instead of at apply time. if existing[:definition] == declaration[:definition] && existing[:type] == declaration[:type] return existing end raise ArgumentError, "#{self}.mongo_search_index #{name_str.inspect} re-declared with a different " \ "definition or type. Each name may have one declaration per class. " \ "Use a unique name for the new index, or update the existing declaration in place." end mongo_search_index_declarations << declaration declaration end |
#mongo_search_index_declarations ⇒ Array<Hash>
Storage for declared search indexes. Each entry is a frozen Hash
with keys :name, :definition, :type.
65 66 67 |
# File 'lib/parse/model/core/search_indexing.rb', line 65 def mongo_search_index_declarations @mongo_search_index_declarations ||= [] end |
#search_indexes_plan ⇒ Hash
Dry-run reconciliation between declared search indexes and what exists on Atlas. Delegates to Schema::SearchIndexMigrator.
192 193 194 |
# File 'lib/parse/model/core/search_indexing.rb', line 192 def search_indexes_plan Parse::Schema::SearchIndexMigrator.new(self).plan end |
#vector_search_index(name, field: nil) ⇒ Hash
Declare a vectorSearch index whose definition is GENERATED from the model's declarations by VectorSearch::IndexDefinition.build (vector path, dimensions, similarity, quantization, agent_searchable filter fields, agent_tenant_scope field), instead of written by hand.
The definition is computed when the migrator plans, not at class
load, so agent_searchable / agent_tenant_scope may be declared in
any order. Nothing is applied until #apply_search_indexes! runs.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/parse/model/core/search_indexing.rb', line 147 def vector_search_index(name, field: nil) name_str = name.to_s unless name_str.match?(INDEX_NAME_PATTERN) raise ArgumentError, "#{self}.vector_search_index name #{name.inspect} must match #{INDEX_NAME_PATTERN.inspect}" end if mongo_search_index_declarations.any? { |d| d[:name] == name_str } raise ArgumentError, "#{self}.vector_search_index #{name_str.inspect} is already declared by " \ "mongo_search_index. Each name may have one declaration per class." end entry = { name: name_str, field: field&.to_sym }.freeze existing = generated_vector_search_indexes.find { |e| e[:name] == name_str } if existing return existing if existing == entry raise ArgumentError, "#{self}.vector_search_index #{name_str.inspect} re-declared for a different field." end generated_vector_search_indexes << entry entry end |