From e4ed1eba33d31098ddfdb35032e148a8fe123d58 Mon Sep 17 00:00:00 2001 From: VitalinaVakulchyk Date: Thu, 8 Apr 2021 13:27:13 +0300 Subject: [PATCH] [Fix #679] Clear cache (#782) --- CHANGELOG.md | 1 + README.md | 2 +- lib/chewy/index/actions.rb | 4 +++ spec/chewy/index/actions_spec.rb | 51 ++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a1581537..e5b0f9beb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Changes + * [#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][]) * [#776](https://github.com/toptal/chewy/pull/776): **(Breaking)** Removal of unnecessary features and integrations ([@Vitalina-Vakulchyk][]): diff --git a/README.md b/README.md index 387b3f88a..cbd2e407e 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Chewy is an ODM (Object Document Mapper), built on top of the [the official Elas * [Elasticsearch compatibility matrix](#elasticsearch-compatibility-matrix) * [Active Record](#active-record) * [Getting Started](#getting-started) - * [Minimal client setting](*minimal-client-setting) + * [Minimal client setting](#minimal-client-setting) * [Elasticsearch](#elasticsearch) * [Index](#index) * [Model](#model) diff --git a/lib/chewy/index/actions.rb b/lib/chewy/index/actions.rb index eac3c7a1d..367d064a8 100644 --- a/lib/chewy/index/actions.rb +++ b/lib/chewy/index/actions.rb @@ -221,6 +221,10 @@ def journal @journal ||= Chewy::Journal.new(self) end + def clear_cache(args = {index: index_name}) + client.indices.clear_cache(args) + end + private def optimize_index_settings(index_name) diff --git a/spec/chewy/index/actions_spec.rb b/spec/chewy/index/actions_spec.rb index e020da9f0..8438c30d6 100644 --- a/spec/chewy/index/actions_spec.rb +++ b/spec/chewy/index/actions_spec.rb @@ -680,4 +680,55 @@ describe '.journal' do specify { expect(DummiesIndex.journal).to be_a(Chewy::Journal) } end + + describe '.clear_cache' do + before do + stub_model(:city) + stub_index(:cities) do + define_type City + end + end + + let(:index_name) { 'test_index' } + let(:index_name_with_prefix) { 'cities_test_index' } + let(:unexisted_index_name) { 'wrong_index' } + + context 'with existing index' do + before do + CitiesIndex.create(index_name) + end + + specify do + expect(CitiesIndex) + .to receive(:clear_cache) + .and_call_original + expect { CitiesIndex.clear_cache({index: index_name_with_prefix}) } + .not_to raise_error Elasticsearch::Transport::Transport::Errors::NotFound + end + end + + context 'with unexisting index' do + specify do + expect(CitiesIndex) + .to receive(:clear_cache) + .and_call_original + expect { CitiesIndex.clear_cache({index: unexisted_index_name}) } + .to raise_error Elasticsearch::Transport::Transport::Errors::NotFound + end + end + + context 'without arguments' do + before do + CitiesIndex.create + end + + specify do + expect(CitiesIndex) + .to receive(:clear_cache) + .and_call_original + expect { CitiesIndex.clear_cache } + .not_to raise_error Elasticsearch::Transport::Transport::Errors::NotFound + end + end + end end