class Elastic::Shims::Populating

Public Class Methods

new(_index, _config, _child) click to toggle source
Calls superclass method Elastic::Shims::Base::new
# File lib/elastic/shims/populating.rb, line 3
def initialize(_index, _config, _child)
  super _child
  @index = _index
  @config = _config
end

Public Instance Methods

handle_result(_raw, _formatter) click to toggle source
Calls superclass method Elastic::Shims::Base#handle_result
# File lib/elastic/shims/populating.rb, line 14
def handle_result(_raw, _formatter)
  result = super
  populate result
  result
end
render(_options = {}) click to toggle source
Calls superclass method Elastic::Shims::Base#render
# File lib/elastic/shims/populating.rb, line 9
def render(_options = {})
  disable_hits_source if populate_by_id?
  super
end

Private Instance Methods

disable_hits_source() click to toggle source
# File lib/elastic/shims/populating.rb, line 22
def disable_hits_source
  child.pick(Elastic::Nodes::Concerns::HitProvider) do |node|
    node.source = false
  end
end
middleware_options() click to toggle source
# File lib/elastic/shims/populating.rb, line 50
def middleware_options
  @middleware_options ||= begin
    @index.definition.middleware_options.merge(@config.middleware_options).freeze
  end
end
populate(_result) click to toggle source
# File lib/elastic/shims/populating.rb, line 28
def populate(_result)
  hits = _result.pick(Elastic::Results::Hit).to_a

  if populate_by_id?
    ids = hits.map(&:id)
    objects = target.find_by_ids(ids, middleware_options)
    objects.each_with_index { |o, i| hits[i].data = o }
  else
    hits.each do |hit|
      hit.data = @index.definition.target.build_from_data(hit.source, middleware_options)
    end
  end
end
populate_by_id?() click to toggle source
# File lib/elastic/shims/populating.rb, line 42
def populate_by_id?
  @index.definition.mode == :index
end
target() click to toggle source
# File lib/elastic/shims/populating.rb, line 46
def target
  @index.definition.target
end