Method: Esse::Index::ClassMethods#import

Defined in:
lib/esse/index/documents.rb

#import(*repo_types, context: {}, eager_include_document_attributes: false, lazy_update_document_attributes: false, suffix: nil, **options) ⇒ Numeric

Resolve collection and index data

Parameters:

  • repos (Array<String>)

    List of repo types. Defaults to all types.

  • options (Hash)

    Hash of paramenters that will be passed along to elasticsearch request

  • [String, (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

Returns:

  • (Numeric)

    The number of documents imported



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/esse/index/documents.rb', line 202

def import(*repo_types, context: {}, eager_include_document_attributes: false, lazy_update_document_attributes: false, suffix: nil, **options)
  repo_types = repo_hash.keys if repo_types.empty?
  count = 0

  repo_hash.slice(*repo_types).each do |repo_name, repo|
    doc_attrs = {eager: [], lazy: []}
    doc_attrs[:eager] = repo.lazy_document_attribute_names(eager_include_document_attributes)
    doc_attrs[:lazy] = repo.lazy_document_attribute_names(lazy_update_document_attributes)
    doc_attrs[:lazy] -= doc_attrs[:eager]

    context ||= {}
    context[:lazy_attributes] = doc_attrs[:eager] if doc_attrs[:eager].any?
    repo.each_serialized_batch(**context) do |batch|
      # Elasticsearch 6.x and older have multiple types per index.
      # This gem supports multiple types per index for backward compatibility, but we recommend to update
      # your elasticsearch to a at least 7.x version and use a single type per index.
      #
      # Note that the repository name will be used as the document type.
      # mapping_default_type
      kwargs = { suffix: suffix, type: repo_name, **options }
      cluster.may_update_type!(kwargs)

      bulk(**kwargs, index: batch)

      doc_attrs[:lazy].each do |attr_name|
        partial_docs = repo.documents_for_lazy_attribute(attr_name, batch.reject(&:ignore_on_index?))
        next if partial_docs.empty?

        bulk(**kwargs, update: partial_docs)
      end

      count += batch.size
    end
  end
  count
end