Class: Chewy::MultiSearch
- Inherits:
- 
      Object
      
        - Object
- Chewy::MultiSearch
 
- 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
- 
  
    
      #queries  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute queries. 
Instance Method Summary collapse
- 
  
    
      #add_query(query)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Adds a query to be performed by the MultiSearch. 
- 
  
    
      #initialize(queries, client: Chewy.client)  ⇒ MultiSearch 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Instantiate a new MultiSearch instance. 
- 
  
    
      #perform  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Performs any unperformed queries. 
- 
  
    
      #responses  ⇒ Array<Chewy::Search::Response> 
    
    
  
  
  
  
  
  
  
  
  
    Performs any unperformed queries and returns the responses for all queries. 
Constructor Details
#initialize(queries, client: Chewy.client) ⇒ MultiSearch
Instantiate a new MultiSearch instance.
| 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
#queries ⇒ Object (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
| 24 25 26 | # File 'lib/chewy/multi_search.rb', line 24 def add_query(query) @queries << query end | 
#perform ⇒ Object
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 | 
#responses ⇒ Array<Chewy::Search::Response>
Performs any unperformed queries and returns the responses for all queries.
| 31 32 33 34 | # File 'lib/chewy/multi_search.rb', line 31 def responses perform queries.map(&:response) end |