Module: Mongoid::SearchIndexable
| Relationships & Source Files | |
| Namespace Children | |
|
Modules:
| |
|
Classes:
| |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
ActiveSupport::Concern
|
|
| Defined in: | lib/mongoid/search_indexable.rb |
Overview
Encapsulates behavior around managing search indexes. This feature is only supported when connected to an Atlas cluster.
Instance Method Summary
-
#auto_embed_search(index: nil, path: nil, limit: 10, num_candidates: nil, filter: nil, exact: false, model: nil, pipeline: []) ⇒ Array<Mongoid::Document>
Performs an Atlas Vector Search query for documents with text similar to this document's stored text field, using auto-embedding.
-
#vector_search(index: nil, path: nil, limit: 10, num_candidates: nil, exact: false, filter: nil, pipeline: []) ⇒ Array<Mongoid::Document>
Performs a vector search for documents similar to this one, using this document's stored embedding as the query vector.
DSL Calls
included
[ GitHub ]53 54 55 56
# File 'lib/mongoid/search_indexable.rb', line 53
included do cattr_accessor :search_index_specs self.search_index_specs = [] end
Instance Method Details
#auto_embed_search(index: nil, path: nil, limit: 10, num_candidates: nil, filter: nil, exact: false, model: nil, pipeline: []) ⇒ Array<Mongoid::Document>
Performs an Atlas Vector Search query for documents with text similar to this document's stored text field, using auto-embedding. The current document is excluded from the results.
# File 'lib/mongoid/search_indexable.rb', line 128
def (index: nil, path: nil, limit: 10, num_candidates: nil, filter: nil, exact: false, model: nil, pipeline: []) # rubocop:disable Metrics/ParameterLists _index, resolved_path = self.class.send(:, index, path) text = public_send(resolved_path) if text.nil? raise ArgumentError, "#{resolved_path} is nil on this document; cannot perform auto-embed search" end self_exclusion = { '$match' => { '_id' => { '$ne' => _id } } } post_pipeline = [ self_exclusion, { '$limit' => limit }, *Array(pipeline) ] effective_candidates = num_candidates || (limit * 10) self.class.( text, index: index, path: path, limit: limit + 1, num_candidates: effective_candidates, filter: filter, exact: exact, model: model, pipeline: post_pipeline ) end
#vector_search(index: nil, path: nil, limit: 10, num_candidates: nil, exact: false, filter: nil, pipeline: []) ⇒ Array<Mongoid::Document>
Performs a vector search for documents similar to this one, using this document's stored embedding as the query vector. The document itself is excluded from the results.
# File 'lib/mongoid/search_indexable.rb', line 82
def vector_search(index: nil, path: nil, limit: 10, num_candidates: nil, exact: false, filter: nil, pipeline: []) # rubocop:disable Metrics/ParameterLists _index, resolved_path = self.class.send(:resolve_vector_index, index, path) query_vector = public_send(resolved_path) if query_vector.nil? raise ArgumentError, "#{resolved_path} is nil on this document; cannot perform vector search" end self_exclusion = { '$match' => { '_id' => { '$ne' => _id } } } post_pipeline = [ self_exclusion, { '$limit' => limit }, *Array(pipeline) ] effective_candidates = num_candidates || (limit * 10) self.class.vector_search( query_vector, index: index, path: path, limit: limit + 1, num_candidates: effective_candidates, exact: exact, filter: filter, pipeline: post_pipeline ) end