Class: Chewy::Index::Import::BulkBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/chewy/index/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 and database, taking their join field values 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(index, to_index: [], delete: [], fields: []) ⇒ BulkBuilder

Returns a new instance of BulkBuilder.

Parameters:

  • index (Chewy::Index)

    desired index

  • to_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/index/import/bulk_builder.rb', line 16

def initialize(index, to_index: [], delete: [], fields: [])
  @index = index
  @to_index = to_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
31
32
# File 'lib/chewy/index/import/bulk_builder.rb', line 26

def bulk_body
  populate_cache

  @bulk_body ||= @to_index.flat_map(&method(:index_entry)).concat(
    @delete.flat_map(&method(:delete_entry))
  ).uniq
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



38
39
40
# File 'lib/chewy/index/import/bulk_builder.rb', line 38

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