class Elastictastic::MultiSearch

Constants

Component

Public Class Methods

count(*scopes) click to toggle source
# File lib/elastictastic/multi_search.rb, line 11
def self.count(*scopes)
  new.count(*scopes).run
end
new() click to toggle source
# File lib/elastictastic/multi_search.rb, line 15
def initialize
  @components = []
end
query(*scopes) click to toggle source
# File lib/elastictastic/multi_search.rb, line 7
def self.query(*scopes)
  new.query(*scopes).run
end

Public Instance Methods

count(*scopes) click to toggle source
# File lib/elastictastic/multi_search.rb, line 27
def count(*scopes)
  components = scopes.flatten.map { |scope| Component.new(scope, 'count') }
  @components.concat(components)
  self
end
query(*scopes) click to toggle source
# File lib/elastictastic/multi_search.rb, line 19
def query(*scopes)
  components = validate_scopes_for_query(scopes.flatten).map do |scope|
    Component.new(scope, 'query_then_fetch')
  end
  @components.concat(components)
  self
end
run() click to toggle source
# File lib/elastictastic/multi_search.rb, line 33
def run
  if @components.any?
    responses = Elastictastic.client.msearch(search_bodies)['responses']
    responses.zip(@components) do |response, component|
      raise ServerError[response['error']] if response['error']
      scope, search_type = component.scope, component.search_type
      case search_type
      when 'query_then_fetch' then scope.response = response
      when 'count' then scope.counts = response
      end
    end
  end
  self
end

Private Instance Methods

search_bodies() click to toggle source
# File lib/elastictastic/multi_search.rb, line 50
def search_bodies
  StringIO.new.tap do |io|
    @components.each do |component|
      scope, search_type = component.scope, component.search_type
      headers = scope.multi_search_headers.
        merge('search_type' => search_type)
      io.puts(Elastictastic.json_encode(headers))
      io.puts(Elastictastic.json_encode(scope.params))
    end
  end.string
end
validate_scopes_for_query(scopes) click to toggle source
# File lib/elastictastic/multi_search.rb, line 62
def validate_scopes_for_query(scopes)
  scopes.each do |scope|
    if scope.params['size'].blank?
      raise ArgumentError, "Multi-search scopes must have an explicit size"
    end
  end
end