Skip to content

Commit

Permalink
[Fix #594] Reindex implementation (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalina-Vakulchyk authored and Ivan Rabotyaga committed Apr 12, 2021
1 parent e4ed1eb commit 67edda2
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ Metrics/BlockLength:

Metrics/ModuleLength:
Exclude:
- 'lib/chewy/rake_helper.rb'
- '**/*_spec.rb'
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

* [#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
* [#679](https://github.com/toptal/chewy/issues/679): Wrapped `Elasticsearch::API::Indices::Actions#clear_cache` with `.clear_cache` in Index class ([@Vitalina-Vakulchyk][])
* [#495](https://github.com/toptal/chewy/issues/495): Ability to change Rails console strategy with `Chewy.console_strategy` ([@Vitalina-Vakulchyk][])
* [#778](https://github.com/toptal/chewy/pull/778): **(Breaking)** Drop support for Ruby 2.5 ([@Vitalina-Vakulchyk][])
Expand Down
12 changes: 12 additions & 0 deletions lib/chewy/index/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ def clear_cache(args = {index: index_name})
client.indices.clear_cache(args)
end

def reindex(source: index_name, dest: index_name)
client.reindex(
{
body:
{
source: {index: source},
dest: {index: dest}
}
}
)
end

private

def optimize_index_settings(index_name)
Expand Down
14 changes: 14 additions & 0 deletions lib/chewy/rake_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,20 @@ def all_indexes
Chewy::Index.descendants - [Chewy::Stash::Journal, Chewy::Stash::Specification]
end

# Reindex data from source index to destination index
#
# @example
# Chewy::RakeHelper.reindex(source: 'users_index', dest: 'cities_index') reindex data from 'users_index' index to 'cities_index'
#
# @param source [String], dest [String] indexes to reindex
def reindex(source:, dest:, output: $stdout)
subscribed_task_stats(output) do
output.puts "Source index is #{source}\nDestination index is #{dest}"
Chewy::Index.reindex(source: source, dest: dest)
output.puts "#{source} index successfully reindexed with #{dest} index data"
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 @@ -47,6 +47,11 @@ namespace :chewy do
Chewy::RakeHelper.sync(except: processed)
end

desc 'Reindex data from source index to destination index'
task :reindex, %i[source dest] => :environment do |_task, args|
Chewy::RakeHelper.reindex(source: args[:source], dest: args[:dest])
end

namespace :parallel do
desc 'Parallel version of `rake chewy:reset`'
task reset: :environment do |_task, args|
Expand Down
71 changes: 71 additions & 0 deletions spec/chewy/index/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -731,4 +731,75 @@
end
end
end

describe '.reindex' do
before do
stub_model(:city)
stub_index(:cities) do
define_type City
end
CitiesIndex.create(source_index)
DummiesIndex.create(dest_index)
end

let(:source_index) { 'source_index' }
let(:source_index_with_prefix) { 'cities_source_index' }
let(:dest_index) { 'dest_index' }
let(:dest_index_with_prefix) { 'dummies_dest_index' }
let(:unexisting_index) { 'wrong_index' }

context 'with existing indexes' do
specify do
expect(CitiesIndex)
.to receive(:reindex)
.and_call_original
expect { CitiesIndex.reindex(source: source_index_with_prefix, dest: dest_index_with_prefix) }
.not_to raise_error
end
end

context 'with unexisting indexes' do
context 'source index' do
specify do
expect(CitiesIndex)
.to receive(:reindex)
.and_call_original
expect { CitiesIndex.reindex(source: unexisting_index, dest: dest_index_with_prefix) }
.to raise_error Elasticsearch::Transport::Transport::Errors::NotFound
end
end

context 'dest index' do
specify do
expect(CitiesIndex)
.to receive(:reindex)
.and_call_original
expect { CitiesIndex.reindex(source: source_index_with_prefix, dest: unexisting_index) }
.not_to raise_error
end
end
end

context 'with missing indexes' do
context 'without dest index' do
specify do
expect(DummiesIndex)
.to receive(:reindex)
.and_call_original
expect { DummiesIndex.reindex(source: source_index_with_prefix) }
.not_to raise_error
end
end

context 'without source index' do
specify do
expect(CitiesIndex)
.to receive(:reindex)
.and_call_original
expect { CitiesIndex.reindex(dest: dest_index_with_prefix) }
.not_to raise_error
end
end
end
end
end
42 changes: 42 additions & 0 deletions spec/chewy/rake_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,46 @@
OUTPUT
end
end

describe '.reindex' do
before do
journal
CitiesIndex.create!
CountriesIndex.create!
end

let(:source_index) { 'cities' }
let(:dest_index) { 'countries' }

context 'with correct arguments' do
specify do
output = StringIO.new
described_class.reindex(source: source_index, dest: dest_index, output: output)
expect(output.string).to match(Regexp.new(<<-OUTPUT, Regexp::MULTILINE))
\\Source index is cities
\\Destination index is countries
cities index successfully reindexed with countries index data
Total: \\d+s\\Z
OUTPUT
end
end

context 'with missing indexes' do
context 'without dest index' do
specify do
output = StringIO.new
expect { described_class.reindex(source: source_index, output: output) }
.to raise_error ArgumentError
end
end

context 'without source index' do
specify do
output = StringIO.new
expect { described_class.reindex(dest: dest_index, output: output) }
.to raise_error ArgumentError
end
end
end
end
end

0 comments on commit 67edda2

Please sign in to comment.