Class: Elasticsearch::Helpers::ScrollHelper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/elasticsearch/helpers/scroll_helper.rb

Overview

Elasticsearch Client Helper for the Scroll API

Instance Method Summary collapse

Constructor Details

#initialize(client, index, body, scroll = '1m') ⇒ ScrollHelper

Create a ScrollHelper

Parameters:

  • client (Elasticsearch::Client)

    (Required) Instance of Elasticsearch client to use.

  • index (String)

    (Required) Index on which to perform the Bulk actions.

  • body (Hash)

    Body parameters to re-use in every scroll request

  • scroll (Time) (defaults to: '1m')

    Specify how long a consistent view of the index should be maintained for scrolled search


34
35
36
37
38
39
# 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

Instance Method Details

#clearObject

Clear Scroll and resets inner documents collection


67
68
69
70
# File 'lib/elasticsearch/helpers/scroll_helper.rb', line 67

def clear
  @client.clear_scroll(body: { scroll_id: @scroll_id }) if @scroll_id
  @scroll_id = nil
end

#each {|document| ... } ⇒ Object

Implementation of each for Enumerable module inclusion

Yield Parameters:

  • document (Hash)

    yields a document found in the search hits.


45
46
47
48
49
50
# File 'lib/elasticsearch/helpers/scroll_helper.rb', line 45

def each(&block)
  until (docs = results).empty?
    docs.each(&block)
  end
  clear
end

#resultsObject

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


55
56
57
58
59
60
61
62
63
# File 'lib/elasticsearch/helpers/scroll_helper.rb', line 55

def results
  if @scroll_id
    scroll_request
  else
    initial_search
  end
rescue StandardError => e
  raise e
end