Class: ElasticRecord::Index::ScrollEnumerator
- Inherits:
-
Object
- Object
- ElasticRecord::Index::ScrollEnumerator
- Defined in:
- lib/elastic_record/index/documents.rb
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
readonly
Returns the value of attribute batch_size.
-
#keep_alive ⇒ Object
readonly
Returns the value of attribute keep_alive.
-
#scroll_id ⇒ Object
readonly
Returns the value of attribute scroll_id.
Instance Method Summary collapse
- #each_slice(&block) ⇒ Object
- #initial_search_response ⇒ Object
-
#initialize(elastic_index, search: nil, scroll_id: nil, keep_alive:, batch_size:) ⇒ ScrollEnumerator
constructor
A new instance of ScrollEnumerator.
- #request_more_hits ⇒ Object
- #request_more_ids ⇒ Object
- #request_next_scroll ⇒ Object
- #total_hits ⇒ Object
Constructor Details
#initialize(elastic_index, search: nil, scroll_id: nil, keep_alive:, batch_size:) ⇒ ScrollEnumerator
Returns a new instance of ScrollEnumerator.
7 8 9 10 11 12 13 |
# File 'lib/elastic_record/index/documents.rb', line 7 def initialize(elastic_index, search: nil, scroll_id: nil, keep_alive:, batch_size:) @elastic_index = elastic_index @search = search @scroll_id = scroll_id @keep_alive = keep_alive @batch_size = batch_size end |
Instance Attribute Details
#batch_size ⇒ Object (readonly)
Returns the value of attribute batch_size.
6 7 8 |
# File 'lib/elastic_record/index/documents.rb', line 6 def batch_size @batch_size end |
#keep_alive ⇒ Object (readonly)
Returns the value of attribute keep_alive.
6 7 8 |
# File 'lib/elastic_record/index/documents.rb', line 6 def keep_alive @keep_alive end |
#scroll_id ⇒ Object (readonly)
Returns the value of attribute scroll_id.
6 7 8 |
# File 'lib/elastic_record/index/documents.rb', line 6 def scroll_id @scroll_id end |
Instance Method Details
#each_slice(&block) ⇒ Object
15 16 17 18 19 |
# File 'lib/elastic_record/index/documents.rb', line 15 def each_slice(&block) while (hits = request_more_hits.hits).any? hits.each_slice(batch_size, &block) end end |
#initial_search_response ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/elastic_record/index/documents.rb', line 44 def initial_search_response @initial_search_response ||= begin = { size: batch_size, scroll: keep_alive } elastic_query = @search.reverse_merge('sort' => '_doc') @elastic_index.search(elastic_query, ) end end |
#request_more_hits ⇒ Object
25 26 27 |
# File 'lib/elastic_record/index/documents.rb', line 25 def request_more_hits SearchHits.from_response(@elastic_index.model, request_next_scroll) end |
#request_more_ids ⇒ Object
21 22 23 |
# File 'lib/elastic_record/index/documents.rb', line 21 def request_more_ids request_more_hits.to_ids end |
#request_next_scroll ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/elastic_record/index/documents.rb', line 29 def request_next_scroll if scroll_id.nil? response = initial_search_response else response = @elastic_index.scroll(scroll_id, keep_alive) end @scroll_id = response['_scroll_id'] response end |
#total_hits ⇒ Object
40 41 42 |
# File 'lib/elastic_record/index/documents.rb', line 40 def total_hits initial_search_response['hits']['total'] end |