class Elasticity::IndexMapper

Public Class Methods

new(document_klass, index_config) click to toggle source
# 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
set_delegates(obj, to) click to toggle source
# 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(ids) click to toggle source

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(documents) click to toggle source

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(documents) click to toggle source

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
create_index() click to toggle source

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
delete(id) click to toggle source

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
delete_index() click to toggle source

Deletes the index

# File lib/elasticity/index_mapper.rb, line 53
def delete_index
  @strategy.delete
end
flush_index() click to toggle source

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
get(id) click to toggle source

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_document(id, document_hash) click to toggle source

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
index_exists?() click to toggle source

Does the index exist?

# File lib/elasticity/index_mapper.rb, line 58
def index_exists?
  !@strategy.missing?
end
map_hit(hit) click to toggle source

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
recreate_index() click to toggle source

Re-creates the index for this document

# File lib/elasticity/index_mapper.rb, line 48
def recreate_index
  @strategy.recreate(@index_config.definition)
end
ref_index_name() click to toggle source

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
refresh_index() click to toggle source

Resfreshes the index, forcing any writes

# File lib/elasticity/index_mapper.rb, line 81
def refresh_index
  @strategy.refresh
end
remap!(retry_delete_on_recoverable_errors: true, retry_delay: 30, max_delay: 600) click to toggle source

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