Class: Elastic::Scroll

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/elastic/scroll.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, index_name, options = {}) ⇒ Scroll

Returns a new instance of Scroll.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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

Instance Method Details

#eachObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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_pageObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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