class Tire::Search::Scan

Performs a “scan/scroll” search request, which obtains a ‘scroll_id` and keeps returning documents matching the passed query (or all documents) in batches.

You may want to iterate over the batches being returned:

search = Tire::Search::Scan.new('articles')
search.each do |results|
  puts results.map(&:title)
end

The scan object has a fully Enumerable-compatible interface, so you may call methods like ‘map` or `each_with_index` on it.

To iterate over individual documents, use the ‘each_document` method:

search.each_document do |document|
  puts document.title
end

You may limit the result set being returned by a regular Tire DSL query (or a hash, if you prefer), passed as a second argument:

search = Tire::Search::Scan.new('articles') do
  query { term 'author.exact', 'John Smith' }
end

The feature is also exposed in the Tire top-level DSL:

search = Tire.scan 'articles' do
  query { term 'author.exact', 'John Smith' }
end

See Elasticsearch documentation for further reference:

Attributes

indices[R]
options[R]

Public Class Methods

new(indices=nil, options={}, &block) click to toggle source
# File lib/goodyear/scan.rb, line 47
def initialize(indices=nil, options={}, &block)
  @indices = Array(indices)
  @options = options.update(:search_type => 'scan', :scroll => '10m')
  @seen    = 0
  @tire_options = options.delete(:tire) || {}
  @search  = Search.new(@indices, @options.merge(@tire_options), &block)
end

Public Instance Methods

__logged(error=nil) click to toggle source
# File lib/goodyear/scan.rb, line 100
def __logged(error=nil)
  if Configuration.logger
    Configuration.logger.log_request 'scroll', nil, to_curl

    took = @json['took']        rescue nil
    code = @response.code       rescue nil
    body = "#{@seen}/#{@total} (#{@seen/@total.to_f*100}%)" rescue nil

    Configuration.logger.log_response code || 'N/A', took || 'N/A', body
  end
end
__perform() click to toggle source
# File lib/goodyear/scan.rb, line 85
def __perform
  @response  = Configuration.client.get [url, params].join, scroll_id
  @json      = MultiJson.decode @response.body
  @results   = Results::Collection.new @json, @options
  @total     = @json['hits']['total'].to_i
  @seen     += @results.size
  @scroll_id = @json['_scroll_id']
  return self
ensure
  __logged
end
each() { |results| ... } click to toggle source
# File lib/goodyear/scan.rb, line 67
def each
  until results.empty?
    yield results.results
    __perform
  end
end
each_document() { |item| ... } click to toggle source
# File lib/goodyear/scan.rb, line 74
def each_document
  until results.empty?
    results.each { |item| yield item }
    __perform
  end
end
json() click to toggle source
# File lib/goodyear/scan.rb, line 59
def json;               @json     || (__perform; @json);                                 end
params() click to toggle source
# File lib/goodyear/scan.rb, line 56
def params;             @options.empty? ? '' : '?' + @options.to_param;                  end
response() click to toggle source
# File lib/goodyear/scan.rb, line 58
def response;           @response || (__perform; @response);                             end
results() click to toggle source
# File lib/goodyear/scan.rb, line 57
def results;            @results  || (__perform; @results);                              end
scroll_id() click to toggle source
# File lib/goodyear/scan.rb, line 63
def scroll_id
  @scroll_id ||= @search.perform.json['_scroll_id']
end
seen() click to toggle source
# File lib/goodyear/scan.rb, line 61
def seen;               @seen     || (__perform; @seen);                                 end
size() click to toggle source
# File lib/goodyear/scan.rb, line 81
def size
  results.size
end
to_a() click to toggle source
# File lib/goodyear/scan.rb, line 97
def to_a;        results; end
Also aliased as: to_ary
to_ary()
Alias for: to_a
to_curl() click to toggle source
# File lib/goodyear/scan.rb, line 98
def to_curl;     %Q|curl -X GET '#{url}?pretty' -d '#{@scroll_id}'|; end
total() click to toggle source
# File lib/goodyear/scan.rb, line 60
def total;              @total    || (__perform; @total);                                end
url() click to toggle source
# File lib/goodyear/scan.rb, line 55
def url;                Configuration.url + "/_search/scroll";                           end