Class: Elastomer::Client::MultiPercolate

Inherits:
Object
  • Object
show all
Defined in:
lib/elastomer/client/multi_percolate.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, params = {}) ⇒ MultiPercolate

Create a new MultiPercolate instance for accumulating percolate actions 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_percolate method



66
67
68
69
70
71
# File 'lib/elastomer/client/multi_percolate.rb', line 66

def initialize(client, params = {})
  @client  = client
  @params  = params

  @actions = []
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



73
74
75
# File 'lib/elastomer/client/multi_percolate.rb', line 73

def client
  @client
end

Instance Method Details

#add_to_actions(action) ⇒ Object

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.



120
121
122
123
124
# File 'lib/elastomer/client/multi_percolate.rb', line 120

def add_to_actions(action)
  action = MultiJson.dump action
  @actions << action
  self
end

#callObject

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.



104
105
106
107
108
109
110
111
# File 'lib/elastomer/client/multi_percolate.rb', line 104

def call
  return if @actions.empty?

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

#count(doc, header = {}) ⇒ Object

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.



95
96
97
98
# File 'lib/elastomer/client/multi_percolate.rb', line 95

def count(doc, header = {})
  add_to_actions(count: @params.merge(header))
  add_to_actions(doc: doc)
end

#percolate(doc, header = {}) ⇒ Object

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.



82
83
84
85
# File 'lib/elastomer/client/multi_percolate.rb', line 82

def percolate(doc, header = {})
  add_to_actions(percolate: @params.merge(header))
  add_to_actions(doc: doc)
end