diff --git a/lib/chewy/journal.rb b/lib/chewy/journal.rb index e012eef9f..fcdc5e2dd 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,12 +82,21 @@ def create end def delete! - Chewy.client.delete_by_query index: index_name, body: { query: { match_all: {} } } - Chewy.wait_for_status + Chewy.client.indices.delete index: index_name + create 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 end