Class: Tire::Search::Multi::Search
- Inherits:
-
Object
- Object
- Tire::Search::Multi::Search
- Defined in:
- lib/tire/multi_search.rb
Overview
Build and perform a multi-search request.
s = Tire::Search::Multi::Search.new 'my-index' do
search :names do
query { match :name, 'john' }
end
search :counts, search_type: 'count' do
query { match :_all, 'john' }
end
search :other, index: 'other-index' do
query { string "first_name:john" }
end
end
You can optionally pass an index and type to the constructor, using them as defaults for searches which don't define them.
Use the #search method to add a search definition to the request, passing it search options as a Hash and the search definition itself using Tire's DSL.
Instance Attribute Summary collapse
-
#indices ⇒ Object
readonly
Returns the value of attribute indices.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
-
#initialize(indices = nil, options = {}, &block) ⇒ Search
constructor
A new instance of Search.
-
#json ⇒ Object
Returns the raw JSON as a Hash.
- #logged(endpoint = '_msearch') ⇒ Object
-
#names ⇒ Object
Returns and array of search definition names.
-
#params ⇒ Object
Serializes the request URL parameters.
- #perform ⇒ Object
-
#response ⇒ Object
Returns the HTTP response.
-
#results ⇒ Object
Returns an enumerable collection of result sets.
-
#search(*args, &block) ⇒ Object
Add a search definition to the multi-search request.
-
#searches(name = nil) ⇒ Object
Without argument, returns the collection of search definitions.
-
#to_array ⇒ Object
Serializes the search definitions as an array of JSON definitions.
- #to_curl ⇒ Object
-
#to_payload ⇒ Object
Serializes the search definitions as a multi-line string payload.
-
#url ⇒ Object
Returns the request URL.
Constructor Details
#initialize(indices = nil, options = {}, &block) ⇒ Search
Returns a new instance of Search.
107 108 109 110 111 112 113 114 115 |
# File 'lib/tire/multi_search.rb', line 107 def initialize(indices=nil, ={}, &block) @indices = Array(indices) @types = Array(.delete(:type)).map { |type| Utils.escape(type) } = @path = ['/', @indices.join(','), @types.join(','), '_msearch'].compact.join('/').squeeze('/') @searches = Tire::Search::Multi::SearchDefinitions.new block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given? end |
Instance Attribute Details
#indices ⇒ Object (readonly)
Returns the value of attribute indices.
105 106 107 |
# File 'lib/tire/multi_search.rb', line 105 def indices @indices end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
105 106 107 |
# File 'lib/tire/multi_search.rb', line 105 def path @path end |
#types ⇒ Object (readonly)
Returns the value of attribute types.
105 106 107 |
# File 'lib/tire/multi_search.rb', line 105 def types @types end |
Instance Method Details
#json ⇒ Object
Returns the raw JSON as a Hash
216 217 218 |
# File 'lib/tire/multi_search.rb', line 216 def json @json || perform and @json end |
#logged(endpoint = '_msearch') ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/tire/multi_search.rb', line 237 def logged(endpoint='_msearch') if Configuration.logger Configuration.logger.log_request endpoint, indices, to_curl took = @json['took'] rescue nil code = @response.code rescue nil if Configuration.logger.level.to_s == 'debug' body = if @json MultiJson.encode( @json, :pretty => Configuration.pretty) else MultiJson.encode( MultiJson.load(@response.body), :pretty => Configuration.pretty) rescue '' end else body = '' end Configuration.logger.log_response code || 'N/A', took || 'N/A', body || 'N/A' end end |
#names ⇒ Object
Returns and array of search definition names
148 149 150 |
# File 'lib/tire/multi_search.rb', line 148 def names @searches.names end |
#params ⇒ Object
Serializes the request URL parameters
181 182 183 184 |
# File 'lib/tire/multi_search.rb', line 181 def params = .dup .empty? ? '' : '?' + .to_param end |
#perform ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/tire/multi_search.rb', line 220 def perform @response = Configuration.client.get(url + params, to_payload) if @response.failure? STDERR.puts "[REQUEST FAILED] #{to_curl}\n" raise SearchRequestFailed, @response.to_s end @json = MultiJson.decode(@response.body) @results = Tire::Search::Multi::Results.new @searches, @json['responses'] return self ensure logged end |
#response ⇒ Object
Returns the HTTP response
210 211 212 |
# File 'lib/tire/multi_search.rb', line 210 def response @response || perform and @response end |
#results ⇒ Object
Returns an enumerable collection of result sets.
You can simply iterate over them:
search.results.each do |results|
puts results.each.map(&:name)
end
To iterate over named result sets, use the each_pair
method:
search.results.each_pair do |name,results|
puts "Search #{name} got #{results.size} results"
end
To get a specific result set:
search.results[:myname]
204 205 206 |
# File 'lib/tire/multi_search.rb', line 204 def results @results || perform and @results end |
#search(*args, &block) ⇒ Object
Add a search definition to the multi-search request.
The usual search options such as search_type
, routing
, etc. can be passed as a Hash,
and the search definition itself should be passed as a block using the Tire DSL.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/tire/multi_search.rb', line 122 def search(*args, &block) = args.pop if .is_a?(Hash) = name = args.pop else name = end name ||= @searches.size ||= {} indices = .delete(:index) || .delete(:indices) @searches << { :name => name, :search => Tire::Search::Search.new(indices, , &block) } end |
#searches(name = nil) ⇒ Object
Without argument, returns the collection of search definitions. With argument, returns a search definition by name or order.
142 143 144 |
# File 'lib/tire/multi_search.rb', line 142 def searches(name=nil) name ? @searches[ name ] : @searches end |
#to_array ⇒ Object
Serializes the search definitions as an array of JSON definitions
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/tire/multi_search.rb', line 154 def to_array @searches.map do |search| header = {} header.update(:index => search.indices.join(',')) unless search.indices.empty? header.update(:type => search.types.join(',')) unless search.types.empty? header.update(:search_type => search.[:search_type]) if search.[:search_type] header.update(:routing => search.[:routing]) if search.[:routing] header.update(:preference => search.[:preference]) if search.[:preference] body = search.to_hash [ header, body ] end.flatten end |
#to_curl ⇒ Object
233 234 235 |
# File 'lib/tire/multi_search.rb', line 233 def to_curl %Q|curl -X GET '#{url}#{params.empty? ? '?' : params.to_s + '&'}pretty' -d '\n#{to_payload}'| end |
#to_payload ⇒ Object
Serializes the search definitions as a multi-line string payload
169 170 171 |
# File 'lib/tire/multi_search.rb', line 169 def to_payload to_array.map { |line| MultiJson.encode(line) }.join("\n") + "\n" end |
#url ⇒ Object
Returns the request URL
175 176 177 |
# File 'lib/tire/multi_search.rb', line 175 def url [ Configuration.url, @path ].join end |