Skip to content

Commit

Permalink
[Fix toptal#692] Update index mapping (toptal#786)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalina-Vakulchyk authored and Çağatay Yücelen committed Jan 28, 2023
1 parent 57ec80a commit 0f1f062
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/chewy/index/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions lib/chewy/rake_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/chewy.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
52 changes: 52 additions & 0 deletions spec/chewy/index/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions spec/chewy/rake_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0f1f062

Please sign in to comment.