Class: Elastomer::Client::MultiSearch

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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



66
67
68
69
70
71
# File 'lib/elastomer/client/multi_search.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_search.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 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.



108
109
110
111
112
# File 'lib/elastomer/client/multi_search.rb', line 108

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

#callObject

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.



91
92
93
94
95
96
97
98
# 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

#search(query, params = {}) ⇒ Object

Add a search to the multi search request. This search will not be executed until the multi_search API call is made.

query - The query body as a Hash params - Parameters Hash

Returns this MultiSearch instance.



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

def search(query, params = {})
  add_to_actions(params)
  add_to_actions(query)
end