class Elasticsearch::Helpers::ScrollHelper

Elasticsearch Client Helper for the Scroll API

@see www.elastic.co/guide/en/elasticsearch/reference/current/scroll-api.html

Public Class Methods

new(client, index, body, scroll = '1m') click to toggle source

Create a ScrollHelper

@param [Elasticsearch::Client] client (Required) Instance of Elasticsearch client to use. @param [String] index (Required) Index on which to perform the Bulk actions. @param [Hash] body Body parameters to re-use in every scroll request @param [Time] scroll Specify how long a consistent view of the index should be maintained for scrolled search

# File lib/elasticsearch/helpers/scroll_helper.rb, line 34
def initialize(client, index, body, scroll = '1m')
  @index = index
  @client = client
  @scroll = scroll
  @body = body
end

Public Instance Methods

clear() click to toggle source

Clear Scroll and resets inner documents collection

# File lib/elasticsearch/helpers/scroll_helper.rb, line 71
def clear
  @client.clear_scroll(body: { scroll_id: @scroll_id }) if @scroll_id
  @docs = []
end
each() { |doc| ... } click to toggle source

Implementation of each for Enumerable module inclusion

@yieldparam document [Hash] yields a document found in the search hits.

# File lib/elasticsearch/helpers/scroll_helper.rb, line 45
def each(&block)
  @docs = []
  @scroll_id = nil
  refresh_docs
  for doc in @docs do
    refresh_docs
    yield doc
  end
  clear
end
results() click to toggle source

Results from a scroll. Can be called repeatedly (e.g. in a loop) to get the scroll pages.

# File lib/elasticsearch/helpers/scroll_helper.rb, line 59
def results
  if @scroll_id
    scroll_request
  else
    initial_search
  end
rescue StandardError => e
  raise e
end

Private Instance Methods

refresh_docs() click to toggle source
# File lib/elasticsearch/helpers/scroll_helper.rb, line 78
def refresh_docs
  @docs ||= []
  @docs << results
  @docs.flatten!
end
scroll_request() click to toggle source
# File lib/elasticsearch/helpers/scroll_helper.rb, line 90
def scroll_request
  @client.scroll(body: {scroll: @scroll, scroll_id: @scroll_id})['hits']['hits']
end