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



71
72
73
74
# 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 {|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
51
52
53
54
# 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

#resultsObject

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



59
60
61
62
63
64
65
66
67
# 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