Class: Chewy::Type::Import::BulkBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/chewy/type/import/bulk_builder.rb

Overview

This class purpose is to build ES client-acceptable bulk request body from the passed objects for index and deletion. It handles parent-child relationships as well by fetching existing documents from ES, taking their _parent field and using it in the bulk body. If fields are passed - it creates partial update entries except for the cases when the type has parent and parent_id has been changed.

Instance Method Summary collapse

Constructor Details

#initialize(type, index: [], delete: [], fields: []) ⇒ BulkBuilder

Returns a new instance of BulkBuilder.

Parameters:

  • type (Chewy::Type)

    desired type

  • index (Array<Object>) (defaults to: [])

    objects to index

  • delete (Array<Object>) (defaults to: [])

    objects or ids to delete

  • fields (Array<Symbol, String>) (defaults to: [])

    and array of fields for documents update



16
17
18
19
20
21
# File 'lib/chewy/type/import/bulk_builder.rb', line 16

def initialize(type, index: [], delete: [], fields: [])
  @type = type
  @index = index
  @delete = delete
  @fields = fields.map!(&:to_sym)
end

Instance Method Details

#bulk_bodyArray<Hash>

Returns ES API-ready bulk requiest body.



26
27
28
29
30
# File 'lib/chewy/type/import/bulk_builder.rb', line 26

def bulk_body
  @bulk_body ||= @index.flat_map(&method(:index_entry)).concat(
    @delete.flat_map(&method(:delete_entry))
  )
end

#index_objects_by_idHash[String => Object]

The only purpose of this method is to cache document ids for all the passed object for index to avoid ids recalculation.

Returns:

  • (Hash[String => Object])

    an ids-objects index hash



36
37
38
# File 'lib/chewy/type/import/bulk_builder.rb', line 36

def index_objects_by_id
  @index_objects_by_id ||= index_object_ids.invert.stringify_keys!
end