Class: ElastomerClient::Client::MultiSearch
- Inherits:
-
Object
- Object
- ElastomerClient::Client::MultiSearch
- Defined in:
- lib/elastomer_client/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
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#add_to_actions(action) ⇒ Object
Internal: Add an action to the pending request.
-
#call ⇒ Object
Execute the multi_search call with the accumulated searches.
-
#initialize(client, params = {}) ⇒ MultiSearch
constructor
Create a new MultiSearch instance for accumulating searches and submitting them all as a single request.
-
#search(query, params = {}) ⇒ Object
Add a search to the multi search request.
Constructor Details
#initialize(client, params = {}) ⇒ MultiSearch
Create a new MultiSearch instance for accumulating searches 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_search method
68 69 70 71 72 73 |
# File 'lib/elastomer_client/client/multi_search.rb', line 68 def initialize(client, params = {}) @client = client @params = params @actions = [] end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
75 76 77 |
# File 'lib/elastomer_client/client/multi_search.rb', line 75 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.
110 111 112 113 114 |
# File 'lib/elastomer_client/client/multi_search.rb', line 110 def add_to_actions(action) action = MultiJson.dump action @actions << action self end |
#call ⇒ Object
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.
93 94 95 96 97 98 99 100 |
# File 'lib/elastomer_client/client/multi_search.rb', line 93 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.
84 85 86 87 |
# File 'lib/elastomer_client/client/multi_search.rb', line 84 def search(query, params = {}) add_to_actions(params) add_to_actions(query) end |