Class: Tire::Search::Scan

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tire/search/scan.rb

Overview

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:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indices = nil, options = {}, &block) ⇒ Scan

Returns a new instance of Scan



47
48
49
50
51
52
# File 'lib/tire/search/scan.rb', line 47

def initialize(indices=nil, options={}, &block)
  @indices = Array(indices)
  @options = options.update(:search_type => 'scan', :scroll => '10m')
  @seen    = 0
  @search  = Search.new(@indices, @options, &block)
end

Instance Attribute Details

#indicesObject (readonly)

Returns the value of attribute indices



45
46
47
# File 'lib/tire/search/scan.rb', line 45

def indices
  @indices
end

#optionsObject (readonly)

Returns the value of attribute options



45
46
47
# File 'lib/tire/search/scan.rb', line 45

def options
  @options
end

#searchObject (readonly)

Returns the value of attribute search



45
46
47
# File 'lib/tire/search/scan.rb', line 45

def search
  @search
end

Instance Method Details

#__logged(error = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tire/search/scan.rb', line 99

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

#__performObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tire/search/scan.rb', line 84

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

#eachObject



66
67
68
69
70
71
# File 'lib/tire/search/scan.rb', line 66

def each
  until results.empty?
    yield results.results
    __perform
  end
end

#each_documentObject



73
74
75
76
77
78
# File 'lib/tire/search/scan.rb', line 73

def each_document
  until results.empty?
    results.each { |item| yield item }
    __perform
  end
end

#jsonObject



58
# File 'lib/tire/search/scan.rb', line 58

def json;               @json     || (__perform; @json);                                 end

#paramsObject



55
# File 'lib/tire/search/scan.rb', line 55

def params;             @options.empty? ? '' : '?' + @options.to_param;                  end

#responseObject



57
# File 'lib/tire/search/scan.rb', line 57

def response;           @response || (__perform; @response);                             end

#resultsObject



56
# File 'lib/tire/search/scan.rb', line 56

def results;            @results  || (__perform; @results);                              end

#scroll_idObject



62
63
64
# File 'lib/tire/search/scan.rb', line 62

def scroll_id
  @scroll_id ||= @search.perform.json['_scroll_id']
end

#seenObject



60
# File 'lib/tire/search/scan.rb', line 60

def seen;               @seen     || (__perform; @seen);                                 end

#sizeObject



80
81
82
# File 'lib/tire/search/scan.rb', line 80

def size
  results.size
end

#to_aObject Also known as: to_ary



96
# File 'lib/tire/search/scan.rb', line 96

def to_a;        results; end

#to_curlObject



97
# File 'lib/tire/search/scan.rb', line 97

def to_curl;     %Q|curl -X GET '#{url}?pretty' -d '#{@scroll_id}'|; end

#totalObject



59
# File 'lib/tire/search/scan.rb', line 59

def total;              @total    || (__perform; @total);                                end

#urlObject



54
# File 'lib/tire/search/scan.rb', line 54

def url;                Configuration.url + "/_search/scroll";                           end