Class: Chewy::Index::Import::BulkRequest

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(index, suffix: nil, bulk_size: nil, **bulk_options) ⇒ BulkRequest

Returns a new instance of BulkRequest.

Parameters:

  • index (Chewy::Index)

    an index for the request

  • suffix (String) (defaults to: nil)

    an index name optional suffix

  • bulk_size (Integer) (defaults to: nil)

    bulk size in bytes

  • bulk_options (Hash)

    options passed to the elasticsearch-api bulk method

Raises:

  • (ArgumentError)


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, **bulk_options)
  @index = index
  @suffix = suffix
  @bulk_size = bulk_size - 1.kilobyte if bulk_size # 1 kilobyte for request header and newlines
  @bulk_options = 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.

Parameters:

  • body (Array<Hash>)

    a standard bulk request body

Returns:

  • (Array<Hash>)

    an array of bulk errors



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