Module: Chewy::Journal::Clean

Defined in:
lib/chewy/journal/clean.rb

Constant Summary collapse

DELETE_BATCH_SIZE =
10_000

Class Method Summary collapse

Class Method Details

.until(time) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/chewy/journal/clean.rb', line 6

def until(time)
  query = Query.new(time, :lte, nil, false).to_h
  search_query = query.merge(fields: ['_id'], size: DELETE_BATCH_SIZE)
  index_name = Journal.index_name

  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: Journal.type_name, _id: id } } }, refresh: true)
  end

  Chewy.wait_for_status
  count
end