class Elastomer::Client::MultiSearch

The MultiSearch class is a helper for accumulating and submitting multi_search API requests. Instances of the MultiSearch class accumulate searches and then issue a single API request to Elasticsearch, which runs all accumulated searches in parallel and returns each result hash aggregated into an array of result hashes.

Instead of instantiating this class directly, use the block form of Client#multi_search.

Attributes

client[R]

Public Class Methods

new(client, params = {}) click to toggle source

Create a new MultiSearch instance for accumulating searches and submitting them all as a single request.

client - Elastomer::Client used for HTTP requests to the server params - Parameters Hash to pass to the Client#multi_search method

# File lib/elastomer/client/multi_search.rb, line 66
def initialize(client, params = {})
  @client  = client
  @params  = params

  @actions = []
end

Public Instance Methods

add_to_actions(action) click to toggle source

Internal: Add an action to the pending request. Actions can be either search params or query bodies. The first action must be a search params hash, followed by a query body, then alternating params and queries.

action - the Hash (params or query) to add to the pending request

Returns this MultiSearch instance.

# File lib/elastomer/client/multi_search.rb, line 108
def add_to_actions(action)
  action = MultiJson.dump action
  @actions << action
  self
end
call() click to toggle source

Execute the multi_search call with the accumulated searches. If the accumulated actions list is empty then no action is taken.

Returns the response body Hash.

# File lib/elastomer/client/multi_search.rb, line 91
def call
  return if @actions.empty?

  body = @actions.join("\n") + "\n"
  client.multi_search(body, @params)
ensure
  @actions.clear
end