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
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 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
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 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
# File lib/elasticsearch/helpers/scroll_helper.rb, line 84 def initial_search response = @client.search(index: @index, scroll: @scroll, body: @body) @scroll_id = response['_scroll_id'] response['hits']['hits'] end
# File lib/elasticsearch/helpers/scroll_helper.rb, line 78 def refresh_docs @docs ||= [] @docs << results @docs.flatten! end
# File lib/elasticsearch/helpers/scroll_helper.rb, line 90 def scroll_request @client.scroll(body: {scroll: @scroll, scroll_id: @scroll_id})['hits']['hits'] end