class ElasticRecord::Index::ScrollEnumerator

Attributes

batch_size[R]
keep_alive[R]
scroll_id[R]

Public Class Methods

new(elastic_index, search: nil, scroll_id: nil, keep_alive:, batch_size:) click to toggle source
# 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

Public Instance Methods

each_slice(&block) click to toggle source
# 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() click to toggle source
# File lib/elastic_record/index/search.rb, line 51
def initial_search_response
  @initial_search_response ||= begin
    search_options = { size: batch_size, scroll: keep_alive }
    elastic_query = @search.reverse_merge('sort' => '_doc')

    @elastic_index.search(elastic_query, search_options)
  end
end
request_more_hits() click to toggle source
# File lib/elastic_record/index/search.rb, line 27
def request_more_hits
  SearchHits.from_response(request_next_scroll)
end
request_more_ids() click to toggle source
# File lib/elastic_record/index/search.rb, line 23
def request_more_ids
  request_more_hits.to_ids
end
request_next_scroll() click to toggle source
# 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() click to toggle source
# File lib/elastic_record/index/search.rb, line 47
def total_hits
  SearchHits.from_response(initial_search_response).total
end