Class: Chewy::Index::Import::BulkRequest
- Inherits:
- 
      Object
      
        - Object
- Chewy::Index::Import::BulkRequest
 
- Defined in:
- lib/chewy/index/import/bulk_request.rb
Overview
Adds additional features to elasticsearch-api bulk method:
- supports Chewy index suffix if necessary;
- supports bulk_size, devides the passed body in chunks and peforms a separate request for each chunk;
- returns only errored document entries from the response if any present.
Instance Method Summary collapse
- 
  
    
      #initialize(index, suffix: nil, bulk_size: nil, **bulk_options)  ⇒ BulkRequest 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of BulkRequest. 
- 
  
    
      #perform(body)  ⇒ Array<Hash> 
    
    
  
  
  
  
  
  
  
  
  
    Performs a bulk request with the passed body, returns empty array if everything is fine and array filled with errored document entries if something went wrong. 
Constructor Details
#initialize(index, suffix: nil, bulk_size: nil, **bulk_options) ⇒ BulkRequest
Returns a new instance of BulkRequest.
| 17 18 19 20 21 22 23 24 | # File 'lib/chewy/index/import/bulk_request.rb', line 17 def initialize(index, suffix: nil, bulk_size: nil, **) @index = index @suffix = suffix @bulk_size = bulk_size - 1.kilobyte if bulk_size # 1 kilobyte for request header and newlines @bulk_options = raise ArgumentError, '`bulk_size` can\'t be less than 1 kilobyte' if @bulk_size && @bulk_size <= 0 end | 
Instance Method Details
#perform(body) ⇒ Array<Hash>
Performs a bulk request with the passed body, returns empty array if everything is fine and array filled with errored document entries if something went wrong.
| 32 33 34 35 36 37 38 39 40 41 42 43 44 | # File 'lib/chewy/index/import/bulk_request.rb', line 32 def perform(body) return [] if body.blank? request_bodies(body).each_with_object([]) do |request_body, results| response = @index.client.bulk(**request_base.merge(body: request_body)) if request_body.present? next unless response.try(:[], 'errors') response_items = (response.try(:[], 'items') || []) .select { |item| item.values.first['error'] } results.concat(response_items) end end |