Class: Chewy::MultiSearch

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

Overview

Chewy::MultiSearch provides an interface for executing multiple queries via the Elasticsearch Multi Search API. When a MultiSearch is performed it wraps the responses from Elasticsearch and assigns them to the appropriate queries.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queries, client: Chewy.client) ⇒ MultiSearch

Instantiate a new MultiSearch instance.

Parameters:

  • queries (Array<Chewy::Search::Request>)
  • [Elasticsearch::Transport::Client] (Hash)

    a customizable set of options



16
17
18
19
# File 'lib/chewy/multi_search.rb', line 16

def initialize(queries, client: Chewy.client)
  @client = client
  @queries = Array(queries)
end

Instance Attribute Details

#queriesObject (readonly)

Returns the value of attribute queries.



9
10
11
# File 'lib/chewy/multi_search.rb', line 9

def queries
  @queries
end

Instance Method Details

#add_query(query) ⇒ Object

Adds a query to be performed by the MultiSearch

Parameters:



24
25
26
# File 'lib/chewy/multi_search.rb', line 24

def add_query(query)
  @queries << query
end

#performObject

Performs any unperformed queries.



37
38
39
40
41
42
43
# File 'lib/chewy/multi_search.rb', line 37

def perform
  unperformed_queries = queries.reject(&:performed?)
  return if unperformed_queries.empty?

  responses = msearch(unperformed_queries)['responses']
  unperformed_queries.zip(responses).map { |query, response| query.response = response }
end

#responsesArray<Chewy::Search::Response>

Performs any unperformed queries and returns the responses for all queries.

Returns:



31
32
33
34
# File 'lib/chewy/multi_search.rb', line 31

def responses
  perform
  queries.map(&:response)
end