Skip to content

Commit

Permalink
Merge pull request #425 from sergey-kintsel/remove-delete-by-query-fr…
Browse files Browse the repository at this point in the history
…om-journal

Remove delete by query plugin dependency from Chewy::Journal
  • Loading branch information
pyromaniac authored Sep 7, 2016
2 parents e4af9da + e3fe627 commit 4635052
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
25 changes: 22 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,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?
Expand Down
8 changes: 5 additions & 3 deletions spec/chewy/journal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4635052

Please sign in to comment.