Skip to content

Commit

Permalink
Remove delete by query plugin dependency from Chewy::Journal
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Kintsel committed Sep 7, 2016
1 parent bf8a74b commit 1e6dd44
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/chewy/journal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Journal
}
}.freeze

DELETE_BATCH_SIZE = 10_000

def initialize(index)
@records = []
@index = index
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 1e6dd44

Please sign in to comment.