class ElastomerClient::Client::MultiPercolate

The MultiPercolate class is a helper for accumulating and submitting multi_percolate API requests. Instances of the MultiPercolate class accumulate percolate actions and then issue a single API request to Elasticsearch, which runs all accumulated percolate actions 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_percolate.

Attributes

client[R]

Public Class Methods

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

Create a new MultiPercolate instance for accumulating percolate actions and submitting them all as a single request.

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

# File lib/elastomer_client/client/multi_percolate.rb, line 68
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 headers or bodies. The first action must be a percolate header, followed by a body, then alternating headers and bodies.

action - the Hash (header or body) to add to the pending request

Returns this MultiPercolate instance.

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

Execute the multi_percolate call with the accumulated percolate actions. If the accumulated actions list is empty then no action is taken.

Returns the response body Hash.

# File lib/elastomer_client/client/multi_percolate.rb, line 106
def call
  return if @actions.empty?

  body = @actions.join("\n") + "\n"
  client.multi_percolate(body, @params)
ensure
  @actions.clear
end
count(doc, header = {}) click to toggle source

Add a percolate acount action to the multi percolate request. This percolate count action will not be executed until the multi_percolate API call is made.

header - A Hash of the index and type, if not provided in the params doc - A Hash of the document

Returns this MultiPercolate instance.

# File lib/elastomer_client/client/multi_percolate.rb, line 97
def count(doc, header = {})
  add_to_actions(count: @params.merge(header))
  add_to_actions(doc:)
end
percolate(doc, header = {}) click to toggle source

Add a percolate action to the multi percolate request. This percolate action will not be executed until the multi_percolate API call is made.

header - A Hash of the index and type, if not provided in the params doc - A Hash of the document

Returns this MultiPercolate instance.

# File lib/elastomer_client/client/multi_percolate.rb, line 84
def percolate(doc, header = {})
  add_to_actions(percolate: @params.merge(header))
  add_to_actions(doc:)
end