diff --git a/lib/chewy/index/actions.rb b/lib/chewy/index/actions.rb index 51d2be917..319f8fa75 100644 --- a/lib/chewy/index/actions.rb +++ b/lib/chewy/index/actions.rb @@ -160,10 +160,10 @@ def #{method} options = {} # # UsersIndex.reset! Time.now.to_i, journal: true # - def reset!(suffix = nil, journal: false) + def reset!(suffix = nil, journal: false, progress: false) if suffix.present? && (indexes = self.indexes).present? create! suffix, alias: false - result = import suffix: suffix, journal: journal + result = import suffix: suffix, journal: journal, progress: progress client.indices.update_aliases body: { actions: [ *indexes.map do |index| { remove: { index: index, alias: index_name } } diff --git a/lib/chewy/rake_helper.rb b/lib/chewy/rake_helper.rb index 68e48c0de..eb6998c43 100644 --- a/lib/chewy/rake_helper.rb +++ b/lib/chewy/rake_helper.rb @@ -54,7 +54,7 @@ def reset_index(*indexes) normalize_indexes(indexes).each do |index| puts "Resetting #{index}" time = Time.now - index.reset!((time.to_f * 1000).round) + index.reset!((time.to_f * 1000).round, progress: ENV['PROGRESS']) if index.journal? Chewy::Journal.create Chewy::Journal::Apply.since(time, only: [index]) diff --git a/lib/chewy/type/import.rb b/lib/chewy/type/import.rb index 6e5d99246..dfb35a7cb 100644 --- a/lib/chewy/type/import.rb +++ b/lib/chewy/type/import.rb @@ -20,6 +20,7 @@ module ClassMethods # UsersIndex::User.import bulk_size: 10.megabytes # import ElasticSearch bulk size in bytes # UsersIndex::User.import consistency: :quorum # explicit write consistency setting for the operation (one, quorum, all) # UsersIndex::User.import replication: :async # explicitly set the replication type (sync, async) + # UsersIndex::User.import progress: true # print progress as import takes place (after each batch) # # See adapters documentation for more details. # @@ -27,6 +28,7 @@ def import(*args) import_options = args.extract_options! import_options.reverse_merge! _default_import_options bulk_options = import_options.reject { |k, _| !BULK_OPTIONS.include?(k) }.reverse_merge!(refresh: true) + total_imported = 0 index.create!(bulk_options.slice(:suffix)) unless index.exists? @@ -42,6 +44,10 @@ def import(*args) fill_payload_import payload, action_objects fill_payload_errors payload, errors if errors.present? + + total_imported += action_objects.values.flatten.length + print "Imported #{total_imported} items\r" if import_options[:progress] + !errors.present? end end diff --git a/spec/chewy/type/import_spec.rb b/spec/chewy/type/import_spec.rb index 5cf3ea3fc..0a227b1c2 100644 --- a/spec/chewy/type/import_spec.rb +++ b/spec/chewy/type/import_spec.rb @@ -129,6 +129,27 @@ city.import dummy_cities, refresh: false end + context ':progress' do + specify do + expect do + city.import dummy_cities, progress: true + end.to output("Imported 3 items\r").to_stdout + end + + specify do + expect do + city.import dummy_cities, progress: true, batch_size: 2 + end.to output("Imported 2 items\rImported 3 items\r").to_stdout + end + + specify do + expect do + city.import dummy_cities, progress: true, batch_size: 1 + end.to output("Imported 1 items\rImported 2 items\rImported 3 items\r").to_stdout + end + end + + context 'scoped' do before do names = %w(name0 name1)