Sha256: 57c57c3ec3387871132a58826849af7b8bc9c1950a268f38141f61e5c14f9d77

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

module Elastic::Commands
  class ImportIndexDocuments < Elastic::Support::Command.new(
    :index, collection: nil, batch_size: 10000, verbose: false
  )
    def perform
      if collection.present?
        target.collect_from(collection, middleware_options) { |obj| queue obj }
      else
        target.collect_all(middleware_options) { |obj| queue obj }
      end
      flush
    end

    private

    def cache
      @cache ||= []
    end

    def queue(_object)
      cache << render_for_es(_object)
      flush if cache.length >= batch_size
    end

    def flush
      unless cache.empty?
        index.connector.bulk_index(cache)
        log_flush(cache.size) if verbose
        cache.clear
      end
    end

    def log_flush(_size)
      @total ||= 0
      @total += _size
      Elastic.logger.info "Imported #{@total} documents"
    end

    def render_for_es(_object)
      index.new(_object).as_elastic_document
    end

    def target
      index.definition.target
    end

    def middleware_options
      index.definition.middleware_options
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
elastic-rails-1.0.4 lib/elastic/commands/import_index_documents.rb
elastic-rails-1.0.3 lib/elastic/commands/import_index_documents.rb
elastic-rails-1.0.2 lib/elastic/commands/import_index_documents.rb
elastic-rails-1.0.1 lib/elastic/commands/import_index_documents.rb
elastic-rails-1.0.0 lib/elastic/commands/import_index_documents.rb