diff --git a/lib/esse/cli/event_listener.rb b/lib/esse/cli/event_listener.rb index 71e14c6..acdddf7 100644 --- a/lib/esse/cli/event_listener.rb +++ b/lib/esse/cli/event_listener.rb @@ -95,6 +95,13 @@ def elasticsearch_bulk(event) end print_message(stats.join(', ') + '.') end + + def elasticsearch_reindex(event) + print_message '[%s] Reindex from %s to %s successfuly completed', + from: colorize(event[:request].dig(:body, :source, :index), :bold), + to: colorize(event[:request].dig(:body, :dest, :index), :bold), + runtime: formatted_runtime(event[:runtime]) + end end end end diff --git a/lib/esse/index/indices.rb b/lib/esse/index/indices.rb index 2b9fa09..148152c 100644 --- a/lib/esse/index/indices.rb +++ b/lib/esse/index/indices.rb @@ -48,7 +48,7 @@ def create_index(suffix: nil, body: nil, settings: nil, **options) # @return [Hash] the elasticsearch response # # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-close.html - def reset_index(suffix: index_suffix, settings: nil, optimize: true, import: true, reindex: false, **options) + def reset_index(suffix: index_suffix, settings: nil, optimize: true, import: true, reindex: false, refresh: nil, **options) cluster.throw_error_when_readonly! suffix ||= Esse.timestamp @@ -69,9 +69,11 @@ def reset_index(suffix: index_suffix, settings: nil, optimize: true, import: tru cluster.api.delete_index(index: index_name) end if import - import(**options, suffix: suffix) - elsif reindex && (_from = indices_pointing_to_alias).any? - # @TODO: Reindex using the reindex API + import(**options, suffix: suffix, refresh: refresh) + elsif reindex && (source_indexes = indices_pointing_to_alias).any? + source_indexes.each do |from| + cluster.api.reindex(**options, body: { source: { index: from }, dest: { index: index_name(suffix: suffix) } }, refresh: refresh) + end end if optimize && import && number_of_replicas != new_number_of_replicas || refresh_interval != new_refresh_interval diff --git a/spec/esse/cli/event_listener_spec.rb b/spec/esse/cli/event_listener_spec.rb index 0055cfb..d96b6ac 100644 --- a/spec/esse/cli/event_listener_spec.rb +++ b/spec/esse/cli/event_listener_spec.rb @@ -222,6 +222,36 @@ end end + describe '.elasticsearch_reindex' do + subject do + described_class['elasticsearch.reindex'].call(event) + end + + let(:event_id) { 'elasticsearch.reindex' } + + let(:event) do + Esse::Events::Event.new(event_id, payload) + end + + let(:payload) do + { + runtime: 1.32, + request: { + body: { + source: { index: 'source_index' }, + dest: { index: 'dest_index' }, + } + } + } + end + + it 'prints message' do + expect { subject }.to output(<<~MSG).to_stdout + [#{formatted_runtime(1.32)}] Reindex from #{colorize('source_index', :bold)} to #{colorize('dest_index', :bold)} successfuly completed + MSG + end + end + def colorize(*args) Esse::Output.colorize(*args) end diff --git a/spec/support/shared_examples/index_reset_index.rb b/spec/support/shared_examples/index_reset_index.rb index 4f5858a..b51c02b 100644 --- a/spec/support/shared_examples/index_reset_index.rb +++ b/spec/support/shared_examples/index_reset_index.rb @@ -52,4 +52,36 @@ expect(GeosIndex.index_exist?(suffix: index_suffix)).to eq(true) end end + + context 'when the old index has data' do + it 'import data from the old index to the new index' do + es_client do |client, _conf, cluster| + GeosIndex.create_index(alias: true, suffix: '2021') + expect { + GeosIndex.reset_index(suffix: index_suffix, import: true) + }.not_to raise_error + + expect(GeosIndex.indices_pointing_to_alias).to eq(["#{GeosIndex.index_name}_#{index_suffix}"]) + expect(GeosIndex.index_exist?(suffix: '2021')).to eq(true) + expect(GeosIndex.index_exist?(suffix: index_suffix)).to eq(true) + expect(GeosIndex.count).to be_positive + end + end + + it 'reindex data from the old index to the new index' do + es_client do |client, _conf, cluster| + GeosIndex.create_index(alias: true, suffix: '2021') + GeosIndex.import(refresh: true) + + expect { + GeosIndex.reset_index(suffix: index_suffix, import: false, reindex: true, refresh: true) + }.not_to raise_error + + expect(GeosIndex.indices_pointing_to_alias).to eq(["#{GeosIndex.index_name}_#{index_suffix}"]) + expect(GeosIndex.index_exist?(suffix: '2021')).to eq(true) + expect(GeosIndex.index_exist?(suffix: index_suffix)).to eq(true) + expect(GeosIndex.count).to be_positive + end + end + end end