class Elasticity::IndexMapper
Public Class Methods
# File lib/elasticity/index_mapper.rb, line 28 def initialize(document_klass, index_config) @document_klass = document_klass @index_config = index_config @strategy = @index_config.strategy.new(@index_config.client, @index_config.fq_index_base_name, @index_config.document_type, @index_config.use_new_timestamp_format, @index_config.include_type_name_on_create) end
# File lib/elasticity/index_mapper.rb, line 3 def self.set_delegates(obj, to) obj.delegate( :document_type, :mapping, :ref_index_name, :create_index, :recreate_index, :delete_index, :index_exists?, :remap!, :flush_index, :refresh_index, :index_document, :search, :get, :delete, :delete_by_search, :bulk_index, :bulk_update, :bulk_delete, :map_hit, to: to ) end
Public Instance Methods
Bulk
delete documents matching provided ids
# File lib/elasticity/index_mapper.rb, line 139 def bulk_delete(ids) @strategy.bulk do |b| ids.each do |id| b.delete(document_type, id) end end end
Bulk
index the provided documents
# File lib/elasticity/index_mapper.rb, line 117 def bulk_index(documents) @strategy.bulk do |b| documents.each do |doc| b.index(document_type, doc._id, doc.to_document) end end end
Bulk
update the specicied attribute of the provided documents
# File lib/elasticity/index_mapper.rb, line 126 def bulk_update(documents) @strategy.bulk do |b| documents.each do |doc| b.update( document_type, doc[:_id], { doc: { doc[:attr_name] => doc[:attr_value] } } ) end end end
Creates the index for this document
# File lib/elasticity/index_mapper.rb, line 43 def create_index @strategy.create_if_undefined(@index_config.definition) end
Removes one specific document from the index.
# File lib/elasticity/index_mapper.rb, line 107 def delete(id) @strategy.delete_document(document_type, id) end
Removes entries based on a search
# File lib/elasticity/index_mapper.rb, line 112 def delete_by_search(search) @strategy.delete_by_query(document_type, search.body) end
Deletes the index
# File lib/elasticity/index_mapper.rb, line 53 def delete_index @strategy.delete end
Flushes the index, forcing any writes note that v7 no longer forces any writes on flush
# File lib/elasticity/index_mapper.rb, line 76 def flush_index @strategy.flush end
Fetches one specific document from the index by ID.
# File lib/elasticity/index_mapper.rb, line 101 def get(id) doc = @strategy.get_document(document_type, id) @document_klass.new(doc["_source"].merge(_id: doc['_id'])) if doc.present? end
Index the given document
# File lib/elasticity/index_mapper.rb, line 86 def index_document(id, document_hash) @strategy.index_document(document_type, id, document_hash) end
Does the index exist?
# File lib/elasticity/index_mapper.rb, line 58 def index_exists? !@strategy.missing? end
Creates a instance of a document from a ElasticSearch hit data.
# File lib/elasticity/index_mapper.rb, line 148 def map_hit(hit) attrs = { _id: hit["_id"] } attrs.merge!(_score: hit["_score"]) attrs.merge!(sort: hit["sort"]) attrs.merge!(hit["_source"]) if hit["_source"] attrs.merge!(matched_queries: hit["matched_queries"]) if hit["matched_queries"] highlighted = nil if hit["highlight"] highlighted_attrs = hit["highlight"].each_with_object({}) do |(name, v), attrs| name = name.gsub(/\..*\z/, '') attrs[name] ||= v end highlighted = @document_klass.new(attrs.merge(highlighted_attrs)) end injected_attrs = attrs.merge({ highlighted: highlighted, highlighted_attrs: highlighted_attrs.try(:keys), _explanation: hit["_explanation"] }) if @document_klass.config.subclasses.present? @document_klass.config.subclasses[hit["_type"].to_sym].constantize.new(injected_attrs) else @document_klass.new(injected_attrs) end end
Re-creates the index for this document
# File lib/elasticity/index_mapper.rb, line 48 def recreate_index @strategy.recreate(@index_config.definition) end
Gets the index name to be used when you need to reference the index somewhere. This depends on the @strategy being used, but it always refers to the search index.
# File lib/elasticity/index_mapper.rb, line 64 def ref_index_name @strategy.ref_index_name end
Resfreshes the index, forcing any writes
# File lib/elasticity/index_mapper.rb, line 81 def refresh_index @strategy.refresh end
Remap retry_delay & max_delay are in seconds
# File lib/elasticity/index_mapper.rb, line 70 def remap!(retry_delete_on_recoverable_errors: true, retry_delay: 30, max_delay: 600) @strategy.remap(@index_config.definition, retry_delete_on_recoverable_errors: retry_delete_on_recoverable_errors, retry_delay: retry_delay, max_delay: max_delay) end
Searches the index using the parameters provided in the body hash, following the same structure Elasticsearch expects. Returns a DocumentSearch object. search_args allows for
explain: boolean to specify we should request _explanation of the query
# File lib/elasticity/index_mapper.rb, line 95 def search(body, search_args = {}) search_obj = Search.build(@index_config.client, @strategy.search_index, document_types, body, search_args) Search::DocumentProxy.new(search_obj, self.method(:map_hit)) end