diff --git a/lib/chewy/journal.rb b/lib/chewy/journal.rb index e012eef9f..68970e732 100644 --- a/lib/chewy/journal.rb +++ b/lib/chewy/journal.rb @@ -12,6 +12,8 @@ class Journal } }.freeze + DELETE_BATCH_SIZE = 10_000 + def initialize(index) @records = [] @index = index @@ -80,13 +82,30 @@ def create end def delete! - Chewy.client.delete_by_query index: index_name, body: { query: { match_all: {} } } - Chewy.wait_for_status + delete or raise Elasticsearch::Transport::Transport::Errors::NotFound + end + + def delete + result = Chewy.client.indices.delete index: index_name + Chewy.wait_for_status if result + result + rescue Elasticsearch::Transport::Transport::Errors::NotFound + false end def clean_until(time) - Chewy.client.delete_by_query index: index_name, body: query(time, :lte, :query) + query = query(time, :lte, :query) + search_query = query.merge(fields: ['_id'], size: DELETE_BATCH_SIZE) + + count = Chewy.client.count(index: index_name, body: query)['count'] + + (count.to_f / DELETE_BATCH_SIZE).ceil.times do + ids = Chewy.client.search(index: index_name, body: search_query)['hits']['hits'].map { |doc| doc['_id'] } + Chewy.client.bulk body: ids.map { |id| { delete: { _index: index_name, _type: type_name, _id: id } } }, refresh: true + end + Chewy.wait_for_status + count end def exists? diff --git a/spec/chewy/journal_spec.rb b/spec/chewy/journal_spec.rb index abdd79566..3af63a0eb 100644 --- a/spec/chewy/journal_spec.rb +++ b/spec/chewy/journal_spec.rb @@ -154,11 +154,13 @@ def timestamp(time) Chewy::Journal.apply_changes_from(time) expect(PlacesIndex::City.all.to_a.length).to eq 2 - Chewy::Journal.clean_until(import_time) + expect(Chewy::Journal.clean_until(import_time)).to eq 7 expect(Chewy.client.count(index: Chewy::Journal.index_name)['count']).to eq 2 - Chewy::Journal.delete! - expect(Chewy.client.count(index: Chewy::Journal.index_name)['count']).to eq 0 + expect(Chewy::Journal.delete!).to be_truthy + expect { Chewy::Journal.delete! }.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound) + expect(Chewy::Journal.delete).to eq false + expect(Chewy::Journal.exists?).to eq false end end end