class Elastic::Scroll

Public Class Methods

new(client, index_name, options = {}) click to toggle source
# File lib/elastic/scroll.rb, line 5
def initialize(client, index_name, options = {})
  options = {
    size: 1_000,
    scroll: '5m',
    body: {},
    stored_fields: []
  }.merge(options)

  @client        = client
  @index_name    = index_name
  @size          = options[:size]
  @scroll        = options[:scroll]
  @body          = options[:body]
  @stored_fields = options[:stored_fields]
end

Public Instance Methods

each() { |doc| ... } click to toggle source
# File lib/elastic/scroll.rb, line 21
def each
  if block_given?
    while (docs = next_page) && docs.any?
      docs.each do |doc|
        begin
          yield(doc) if block_given?
        rescue => ex
          if defined?(Raven)
            Raven.extra_context(document: doc)
          end
          raise ex
        end
      end
    end
  else
    to_enum(:each)
  end
end
next_page() click to toggle source
# File lib/elastic/scroll.rb, line 40
def next_page
  response =
    if @scroll_id
      @client.scroll(scroll_params)
    else
      @client.search(initial_params)
    end

  if response
    @scroll_id = response['_scroll_id']
    hits       = response['hits']['hits']

    clear! if !hits || hits.empty?

    hits
  else
    clear!
  end

rescue
  clear!
end

Private Instance Methods

clear!() click to toggle source
# File lib/elastic/scroll.rb, line 84
def clear!
  @client.clear_scroll(scroll_id: @scroll_id) if @scroll_id
end
initial_params() click to toggle source
# File lib/elastic/scroll.rb, line 65
def initial_params
  {
    index:  @index_name,
    size:   @size,
    scroll: @scroll,
    body:   @body,
    stored_fields: @stored_fields
  }.delete_if { |_, v| !v || (v.respond_to?(:empty?) && v.empty?) }
end
scroll_params() click to toggle source
# File lib/elastic/scroll.rb, line 75
def scroll_params
  {
    body: {
      scroll_id: @scroll_id,
      scroll:    @scroll
    }
  }
end