diff --git a/CHANGELOG.md b/CHANGELOG.md index a4d43c4cb..20354f0eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ ### Changes + * [#692](https://github.com/toptal/chewy/issues/692): Add `.update_mapping` to Index class ([@Vitalina-Vakulchyk][]): + * Wrapped Elasticsearch gem `.put_mapping` with `.update_mapping` in Index class + * Add `rake chewy:update_mapping` task * [#594](https://github.com/toptal/chewy/issues/594): Add `.reindex` to Index class ([@Vitalina-Vakulchyk][]): * Wrapped Elasticsearch gem `.reindex` with `.reindex` in Index class * Add `rake chewy:reindex` task diff --git a/lib/chewy/index/actions.rb b/lib/chewy/index/actions.rb index 03f7ab4b9..a7bcc517c 100644 --- a/lib/chewy/index/actions.rb +++ b/lib/chewy/index/actions.rb @@ -237,6 +237,19 @@ def reindex(source: index_name, dest: index_name) ) end + # Adds new fields to an existing data stream or index. + # Change the search settings of existing fields. + # + # @example + # Chewy.client.update_mapping('cities', {properties: {new_field: {type: :text}}}) + # + def update_mapping(name = index_name, body = mappings_hash) + client.indices.put_mapping( + index: name, + body: body + )['acknowledged'] + end + private def optimize_index_settings(index_name) diff --git a/lib/chewy/rake_helper.rb b/lib/chewy/rake_helper.rb index 489031d03..b7e295cee 100644 --- a/lib/chewy/rake_helper.rb +++ b/lib/chewy/rake_helper.rb @@ -217,6 +217,21 @@ def reindex(source:, dest:, output: $stdout) end end + # Adds new fields to an existing data stream or index. + # Change the search settings of existing fields. + # + # @example + # Chewy::RakeHelper.update_mapping('cities', {properties: {new_field: {type: :text}}}) update 'cities' index with new_field of text type + # + # @param name [String], body_hash [Hash] index name and body hash to update + def update_mapping(name:, output: $stdout) + subscribed_task_stats(output) do + output.puts "Index name is #{name}" + Chewy::Index.update_mapping(name) + output.puts "#{name} index successfully updated" + end + end + def normalize_indexes(*identifiers) identifiers.flatten(1).map { |identifier| normalize_index(identifier) } end diff --git a/lib/tasks/chewy.rake b/lib/tasks/chewy.rake index a3477a405..07e3cd0bd 100644 --- a/lib/tasks/chewy.rake +++ b/lib/tasks/chewy.rake @@ -52,6 +52,11 @@ namespace :chewy do Chewy::RakeHelper.reindex(source: args[:source], dest: args[:dest]) end + desc 'Update mapping of exising index with body hash' + task :update_mapping, %i[index_name] => :environment do |_task, args| + Chewy::RakeHelper.update_mapping(name: args[:name]) + end + namespace :parallel do desc 'Parallel version of `rake chewy:reset`' task reset: :environment do |_task, args| diff --git a/spec/chewy/index/actions_spec.rb b/spec/chewy/index/actions_spec.rb index 719430b61..9a943940d 100644 --- a/spec/chewy/index/actions_spec.rb +++ b/spec/chewy/index/actions_spec.rb @@ -802,4 +802,56 @@ end end end + + describe 'update_mapping' do + before do + stub_model(:city) + stub_index(:cities) do + define_type City + end + CitiesIndex.create + end + + let(:index_name) { CitiesIndex.index_name } + let(:body_hash) { {properties: {new_field: {type: :text}}} } + let(:unexisting_index) { 'wrong_index' } + let(:empty_body_hash) { {} } + + context 'with existing index' do + specify do + expect { CitiesIndex.update_mapping(index_name, body_hash) } + .not_to raise_error + end + end + + context 'with unexisting arguments' do + context 'index name' do + specify do + expect { CitiesIndex.update_mapping(unexisting_index, body_hash) } + .to raise_error Elasticsearch::Transport::Transport::Errors::NotFound + end + end + + context 'body hash' do + specify do + expect { CitiesIndex.update_mapping(index_name, empty_body_hash) } + .not_to raise_error + end + end + end + + context 'with only argument' do + specify do + expect { CitiesIndex.update_mapping(index_name) } + .not_to raise_error + end + end + + context 'without arguments' do + specify do + expect { CitiesIndex.update_mapping } + .not_to raise_error + end + end + end end diff --git a/spec/chewy/rake_helper_spec.rb b/spec/chewy/rake_helper_spec.rb index 830d7de80..19b471557 100644 --- a/spec/chewy/rake_helper_spec.rb +++ b/spec/chewy/rake_helper_spec.rb @@ -435,4 +435,34 @@ end end end + + describe '.update_mapping' do + before do + journal + CitiesIndex.create! + end + + let(:index_name) { CitiesIndex.index_name } + let(:unexisting_index) { 'wrong_index' } + + context 'with existing index' do + specify do + output = StringIO.new + described_class.update_mapping(name: index_name, output: output) + expect(output.string).to match(Regexp.new(<<-OUTPUT, Regexp::MULTILINE)) +\\Index name is cities +cities index successfully updated +Total: \\d+s\\Z + OUTPUT + end + end + + context 'with unexisting index name' do + specify do + output = StringIO.new + expect { described_class.update_mapping(name: unexisting_index, output: output) } + .to raise_error Elasticsearch::Transport::Transport::Errors::NotFound + end + end + end end