Class: ElasticRecord::Index::ScrollEnumerator
- Inherits:
-
Object
- Object
- ElasticRecord::Index::ScrollEnumerator
- Defined in:
- lib/elastic_record/index/search.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/search.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/search.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/search.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/search.rb', line 6 def scroll_id @scroll_id end |
Instance Method Details
#each_slice(&block) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/elastic_record/index/search.rb', line 15 def each_slice(&block) while (hits = request_more_hits.hits).any? hits.each_slice(batch_size, &block) end @elastic_index.delete_scroll(scroll_id) end |
#initial_search_response ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/elastic_record/index/search.rb', line 51 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
27 28 29 |
# File 'lib/elastic_record/index/search.rb', line 27 def request_more_hits SearchHits.from_response(request_next_scroll) end |
#request_more_ids ⇒ Object
23 24 25 |
# File 'lib/elastic_record/index/search.rb', line 23 def request_more_ids request_more_hits.to_ids end |
#request_next_scroll ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/elastic_record/index/search.rb', line 31 def request_next_scroll if scroll_id response = @elastic_index.scroll(scroll_id, keep_alive) if response['_scroll_id'] != scroll_id @elastic_index.delete_scroll(scroll_id) end else response = initial_search_response end @scroll_id = response['_scroll_id'] response end |
#total_hits ⇒ Object
47 48 49 |
# File 'lib/elastic_record/index/search.rb', line 47 def total_hits SearchHits.from_response(initial_search_response).total end |