Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to print progress while an index imports #470

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/chewy/index/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }
Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/rake_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
6 changes: 6 additions & 0 deletions lib/chewy/type/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ 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.
#
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?

Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/chewy/type/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down