Class: Tire::Search::Scan
- Inherits:
-
Object
- Object
- Tire::Search::Scan
- 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
-
#indices ⇒ Object
readonly
Returns the value of attribute indices.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#search ⇒ Object
readonly
Returns the value of attribute search.
Instance Method Summary collapse
- #__logged(error = nil) ⇒ Object
- #__perform ⇒ Object
- #each ⇒ Object
- #each_document ⇒ Object
-
#initialize(indices = nil, options = {}, &block) ⇒ Scan
constructor
A new instance of Scan.
- #json ⇒ Object
- #params ⇒ Object
- #response ⇒ Object
- #results ⇒ Object
- #scroll_id ⇒ Object
- #seen ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object (also: #to_ary)
- #to_curl ⇒ Object
- #total ⇒ Object
- #url ⇒ Object
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, ={}, &block) @indices = Array(indices) @options = .update(:search_type => 'scan', :scroll => '10m') @seen = 0 @search = Search.new(@indices, @options, &block) end |
Instance Attribute Details
#indices ⇒ Object (readonly)
Returns the value of attribute indices.
45 46 47 |
# File 'lib/tire/search/scan.rb', line 45 def indices @indices end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
45 46 47 |
# File 'lib/tire/search/scan.rb', line 45 def @options end |
#search ⇒ Object (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 |
#__perform ⇒ Object
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 |
#each ⇒ Object
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_document ⇒ Object
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 |
#json ⇒ Object
58 |
# File 'lib/tire/search/scan.rb', line 58 def json; @json || (__perform; @json); end |
#params ⇒ Object
55 |
# File 'lib/tire/search/scan.rb', line 55 def params; @options.empty? ? '' : '?' + @options.to_param; end |
#response ⇒ Object
57 |
# File 'lib/tire/search/scan.rb', line 57 def response; @response || (__perform; @response); end |
#results ⇒ Object
56 |
# File 'lib/tire/search/scan.rb', line 56 def results; @results || (__perform; @results); end |
#scroll_id ⇒ Object
62 63 64 |
# File 'lib/tire/search/scan.rb', line 62 def scroll_id @scroll_id ||= @search.perform.json['_scroll_id'] end |
#seen ⇒ Object
60 |
# File 'lib/tire/search/scan.rb', line 60 def seen; @seen || (__perform; @seen); end |
#size ⇒ Object
80 81 82 |
# File 'lib/tire/search/scan.rb', line 80 def size results.size end |
#to_a ⇒ Object Also known as: to_ary
96 |
# File 'lib/tire/search/scan.rb', line 96 def to_a; results; end |
#to_curl ⇒ Object
97 |
# File 'lib/tire/search/scan.rb', line 97 def to_curl; %Q|curl -X GET '#{url}?pretty' -d '#{@scroll_id}'|; end |
#total ⇒ Object
59 |
# File 'lib/tire/search/scan.rb', line 59 def total; @total || (__perform; @total); end |
#url ⇒ Object
54 |
# File 'lib/tire/search/scan.rb', line 54 def url; Configuration.url + "/_search/scroll"; end |