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:
# 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
# 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
# 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
# File lib/tire/search/scan.rb, line 66 def each until results.empty? yield results.results __perform end end
# File lib/tire/search/scan.rb, line 73 def each_document until results.empty? results.each { |item| yield item } __perform end end
# File lib/tire/search/scan.rb, line 58 def json; @json || (__perform; @json); end
# File lib/tire/search/scan.rb, line 55 def params; @options.empty? ? '' : '?' + @options.to_param; end
# File lib/tire/search/scan.rb, line 57 def response; @response || (__perform; @response); end
# File lib/tire/search/scan.rb, line 56 def results; @results || (__perform; @results); end
# File lib/tire/search/scan.rb, line 62 def scroll_id @scroll_id ||= @search.perform.json['_scroll_id'] end
# File lib/tire/search/scan.rb, line 60 def seen; @seen || (__perform; @seen); end
# File lib/tire/search/scan.rb, line 80 def size results.size end
# File lib/tire/search/scan.rb, line 96 def to_a; results; end
# File lib/tire/search/scan.rb, line 97 def to_curl; %Qcurl -X GET '#{url}?pretty' -d '#{@scroll_id}'|; end
# File lib/tire/search/scan.rb, line 59 def total; @total || (__perform; @total); end
# File lib/tire/search/scan.rb, line 54 def url; Configuration.url + "/_search/scroll"; end