Class: Tire::Index
- Inherits:
-
Object
- Object
- Tire::Index
- Defined in:
- lib/tire/index.rb
Constant Summary collapse
- SUPPORTED_META_PARAMS_FOR_BULK =
[:_routing, :_ttl, :_version, :_version_type, :_percolate, :_parent, :_timestamp]
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #add_alias(alias_name, configuration = {}) ⇒ Object
- #aliases(alias_name = nil) ⇒ Object
- #analyze(text, options = {}) ⇒ Object
-
#bulk(action, documents, options = {}) ⇒ Object
Performs a bulk request.
- #bulk_create(documents, options = {}) ⇒ Object
- #bulk_delete(documents, options = {}) ⇒ Object
- #bulk_store(documents, options = {}) ⇒ Object
- #bulk_update(documents, options = {}) ⇒ Object
- #close(options = {}) ⇒ Object
- #convert_document_to_json(document) ⇒ Object
- #create(options = {}) ⇒ Object
- #delete ⇒ Object
- #delete_mapping(type) ⇒ Object
- #exists? ⇒ Boolean
- #get_id_from_document(document) ⇒ Object
- #get_mapping ⇒ Object
- #get_type_from_document(document, options = {}) ⇒ Object
- #import(klass_or_collection, options = {}) ⇒ Object
-
#initialize(name, &block) ⇒ Index
constructor
A new instance of Index.
- #logged(endpoint = '/', curl = '') ⇒ Object
-
#mapping(*args) ⇒ Object
Get or update the index mapping.
-
#mapping!(*args) ⇒ Object
Raises an exception for unsuccessful responses.
- #open(options = {}) ⇒ Object
- #percolate(*args, &block) ⇒ Object
- #put_mapping(type, mapping) ⇒ Object
- #refresh ⇒ Object
- #register_percolator_query(name, options = {}, &block) ⇒ Object
- #reindex(name, options = {}, &block) ⇒ Object
- #remove(*args) ⇒ Object
- #remove_alias(alias_name) ⇒ Object
- #retrieve(type, id, options = {}) ⇒ Object
- #settings ⇒ Object
- #store(*args) ⇒ Object
- #unregister_percolator_query(name) ⇒ Object
- #update(type, id, payload = {}, options = {}) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(name, &block) ⇒ Index
Returns a new instance of Index.
8 9 10 11 |
# File 'lib/tire/index.rb', line 8 def initialize(name, &block) @name = name block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given? end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/tire/index.rb', line 6 def name @name end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
6 7 8 |
# File 'lib/tire/index.rb', line 6 def response @response end |
Instance Method Details
#add_alias(alias_name, configuration = {}) ⇒ Object
45 46 47 |
# File 'lib/tire/index.rb', line 45 def add_alias(alias_name, configuration={}) Alias.create(configuration.merge( :name => alias_name, :index => @name ) ) end |
#aliases(alias_name = nil) ⇒ Object
53 54 55 |
# File 'lib/tire/index.rb', line 53 def aliases(alias_name=nil) alias_name ? Alias.all(@name).select { |a| a.name == alias_name }.first : Alias.all(@name) end |
#analyze(text, options = {}) ⇒ Object
418 419 420 421 422 423 424 425 426 427 |
# File 'lib/tire/index.rb', line 418 def analyze(text, ={}) = {:pretty => true}.update() params = .to_param @response = Configuration.client.get "#{url}/_analyze?#{params}", text @response.success? ? MultiJson.decode(@response.body) : false ensure curl = %Q|curl -X GET "#{url}/_analyze?#{params}" -d '#{text}'| logged('_analyze', curl) end |
#bulk(action, documents, options = {}) ⇒ Object
Performs a bulk request
@myindex.bulk :index, [ {id: 1, title: 'One'}, { id: 2, title: 'Two', _version: 3 } ], refresh: true
Pass the action (index
, create
, delete
, update
) as the first argument, the collection of documents as
the second argument, and URL parameters as the last option.
Any meta information contained in documents (such as _routing
or _parent
) is extracted
and added to the "header" line.
Shortcut methods bulk_store
, bulk_delete
, bulk_create
, and bulk_update
are available.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/tire/index.rb', line 165 def bulk(action, documents, ={}) return false if documents.empty? # TODO: A more Ruby-like DSL notation should be supported: # # Tire.index('myindex').bulk do # create id: 1, title: 'bar', _routing: 'abc' # delete id: 1 # # ... # end payload = documents.map do |document| type = get_type_from_document(document, :escape => false) # Do not URL-escape the _type id = get_id_from_document(document) if ENV['DEBUG'] STDERR.puts "[ERROR] Document #{document.inspect} does not have ID" unless id end if action.to_sym == :update raise ArgumentError, "Cannot update without document type" unless type raise ArgumentError, "Cannot update without document ID" unless id raise ArgumentError, "Update requires a hash document" unless document.respond_to?(:to_hash) document = document.to_hash raise ArgumentError, "Update requires either a script or a partial document" unless document[:script] || document[:doc] end header = { action.to_sym => { :_index => name, :_type => type, :_id => id } } header[action.to_sym].update({:_retry_on_conflict => [:retry_on_conflict]}) if [:retry_on_conflict] if document.respond_to?(:to_hash) && doc_hash = document.to_hash = doc_hash.select do |k,v| [ :_parent, :_percolate, :_retry_on_conflict, :_routing, :_timestamp, :_ttl, :_version, :_version_type].include?(k) end # Normalize Ruby 1.8 and Ruby 1.9 Hash#select behaviour = Hash[] unless .is_a?(Hash) # meta = SUPPORTED_META_PARAMS_FOR_BULK.inject({}) { |hash, param| # value = doc_hash.delete(param) # hash[param] = value unless !value || value.empty? # hash # } header[action.to_sym].update() end if action.to_sym == :update document.keep_if do |k,_| [ :doc, :upsert, :doc_as_upsert, :script, :params, :lang ].include?(k) end end output = [] output << MultiJson.encode(header) output << convert_document_to_json(document) unless action.to_s == 'delete' output.join("\n") end payload << "" tries = 5 count = 0 begin params = {} params[:consistency] = .delete(:consistency) params[:refresh] = .delete(:refresh) params = params.reject { |name,value| !value } params_encoded = params.empty? ? '' : "?#{params.to_param}" @response = Configuration.client.post("#{url}/_bulk#{params_encoded}", payload.join("\n")) raise RuntimeError, "#{@response.code} > #{@response.body}" if @response && @response.failure? @response rescue StandardError => error if count < tries count += 1 STDERR.puts "[ERROR] #{error.}, retrying (#{count})..." retry else STDERR.puts "[ERROR] Too many exceptions occured, giving up. The HTTP response was: #{error.}" raise if [:raise] end ensure data = Configuration.logger && Configuration.logger.level.to_s == 'verbose' ? payload.join("\n") : '... data omitted ...' curl = %Q|curl -X POST "#{url}/_bulk" --data-binary '#{data}'| logged('_bulk', curl) end end |
#bulk_create(documents, options = {}) ⇒ Object
268 269 270 |
# File 'lib/tire/index.rb', line 268 def bulk_create(documents, ={}) bulk :create, documents, end |
#bulk_delete(documents, options = {}) ⇒ Object
276 277 278 |
# File 'lib/tire/index.rb', line 276 def bulk_delete(documents, ={}) bulk :delete, documents, end |
#bulk_store(documents, options = {}) ⇒ Object
272 273 274 |
# File 'lib/tire/index.rb', line 272 def bulk_store(documents, ={}) bulk :index, documents, end |
#bulk_update(documents, options = {}) ⇒ Object
280 281 282 |
# File 'lib/tire/index.rb', line 280 def bulk_update(documents, ={}) bulk :update, documents, end |
#close(options = {}) ⇒ Object
409 410 411 412 413 414 415 416 |
# File 'lib/tire/index.rb', line 409 def close(={}) @response = Configuration.client.post "#{url}/_close", MultiJson.encode() MultiJson.decode(@response.body)['ok'] ensure curl = %Q|curl -X POST "#{url}/_close"| logged('_close', curl) end |
#convert_document_to_json(document) ⇒ Object
523 524 525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/tire/index.rb', line 523 def convert_document_to_json(document) document = case when document.is_a?(String) if ENV['DEBUG'] Tire.warn "Passing the document as JSON string has been deprecated, " + "please pass an object which responds to `to_indexed_json` or a plain Hash." end document when document.respond_to?(:to_indexed_json) then document.to_indexed_json else raise ArgumentError, "Please pass a JSON string or object with a 'to_indexed_json' method," + "'#{document.class}' given." end end |
#create(options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/tire/index.rb', line 35 def create(={}) @options = @response = Configuration.client.post url, MultiJson.encode() @response.success? ? @response : false ensure curl = %Q|curl -X POST #{url} -d '#{MultiJson.encode(, :pretty => Configuration.pretty)}'| logged('CREATE', curl) end |
#delete ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/tire/index.rb', line 26 def delete @response = Configuration.client.delete url @response.success? ensure curl = %Q|curl -X DELETE #{url}| logged('DELETE', curl) end |
#delete_mapping(type) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/tire/index.rb', line 108 def delete_mapping(type) url = "#{self.url}/#{type}" @response = Configuration.client.delete(url) @response.success? ensure curl = %Q|curl -X DELETE "#{url}"| logged("DELETE MAPPING #{type}", curl) end |
#exists? ⇒ Boolean
17 18 19 20 21 22 23 24 |
# File 'lib/tire/index.rb', line 17 def exists? @response = Configuration.client.head("#{url}") @response.success? ensure curl = %Q|curl -I "#{url}"| logged('HEAD', curl) end |
#get_id_from_document(document) ⇒ Object
511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/tire/index.rb', line 511 def get_id_from_document(document) old_verbose, $VERBOSE = $VERBOSE, nil # Silence Object#id deprecation warnings id = case when document.is_a?(Hash) document[:_id] || document['_id'] || document[:id] || document['id'] when document.respond_to?(:id) && document.id != document.object_id document.id.to_s end $VERBOSE = old_verbose id end |
#get_mapping ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/tire/index.rb', line 80 def get_mapping @response = Configuration.client.get("#{url}/_mapping") result = MultiJson.decode(@response.body)[@name] @response.success? ? result : false ensure curl = %Q|curl -X GET "#{url}/_mapping?pretty"| logged("GET MAPPING", curl) end |
#get_type_from_document(document, options = {}) ⇒ Object
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/tire/index.rb', line 491 def get_type_from_document(document, ={}) = {:escape => true}.merge() old_verbose, $VERBOSE = $VERBOSE, nil # Silence Object#type deprecation warnings type = case when document.respond_to?(:document_type) document.document_type when document.is_a?(Hash) document[:_type] || document['_type'] || document[:type] || document['type'] when document.respond_to?(:_type) document._type when document.respond_to?(:type) && document.type != document.class document.type end $VERBOSE = old_verbose type ||= 'document' [:escape] ? Utils.escape(type) : type end |
#import(klass_or_collection, options = {}) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/tire/index.rb', line 284 def import(klass_or_collection, ={}) case when method = .delete(:method) = {:page => 1, :per_page => 1000}.merge while (documents = klass_or_collection.send(method.to_sym, .merge(:page => [:page]))) \ && documents.to_a.length > 0 documents = yield documents if block_given? bulk_store documents, GC.start [:page] += 1 end when klass_or_collection.respond_to?(:map) documents = block_given? ? yield(klass_or_collection) : klass_or_collection bulk_store documents, else raise ArgumentError, "Please pass either an Enumerable compatible class, or a collection object " + "with a method for fetching records in batches (such as 'paginate')." end end |
#logged(endpoint = '/', curl = '') ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/tire/index.rb', line 468 def logged(endpoint='/', curl='') if Configuration.logger error = $! Configuration.logger.log_request endpoint, @name, curl code = @response ? @response.code : error.class rescue 'N/A' if Configuration.logger.level.to_s == 'debug' body = if @response && @response.body && !@response.body.to_s.empty? MultiJson.encode( MultiJson.load(@response.body), :pretty => Configuration.pretty) elsif error && error. && !error..to_s.empty? MultiJson.encode( MultiJson.load(error.), :pretty => Configuration.pretty) rescue '' else '' end else body = '' end Configuration.logger.log_response code, nil, body end end |
#mapping(*args) ⇒ Object
Get or update the index mapping
Without arguments, returns the index mapping as a Hash
When passed arguments, attempts to update the index mapping:
index.mapping 'article', properties: { body: { type: "string" } }
You can pass the ignore_conflicts
option as a part of the Hash:
index.mapping 'article', properties: { body: { type: "string" } }, ignore_conflicts: true
69 70 71 |
# File 'lib/tire/index.rb', line 69 def mapping(*args) args.empty? ? get_mapping : put_mapping(*args) end |
#mapping!(*args) ⇒ Object
Raises an exception for unsuccessful responses
75 76 77 78 |
# File 'lib/tire/index.rb', line 75 def mapping!(*args) mapping(*args) raise RuntimeError, response.body unless response.success? end |
#open(options = {}) ⇒ Object
399 400 401 402 403 404 405 406 407 |
# File 'lib/tire/index.rb', line 399 def open(={}) # TODO: Remove the duplication in the execute > rescue > ensure chain @response = Configuration.client.post "#{url}/_open", MultiJson.encode() MultiJson.decode(@response.body)['ok'] ensure curl = %Q|curl -X POST "#{url}/_open"| logged('_open', curl) end |
#percolate(*args, &block) ⇒ Object
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/tire/index.rb', line 449 def percolate(*args, &block) document = args.shift type = get_type_from_document(document) document = MultiJson.decode convert_document_to_json(document) query = Search::Query.new(&block).to_hash if block_given? payload = { :doc => document } payload.update( :query => query ) if query @response = Configuration.client.get "#{url}/#{type}/_percolate", MultiJson.encode(payload) MultiJson.decode(@response.body)['matches'] ensure curl = %Q|curl -X GET "#{url}/#{type}/_percolate?pretty" -d '#{MultiJson.encode(payload, :pretty => Configuration.pretty)}'| logged('_percolate', curl) end |
#put_mapping(type, mapping) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/tire/index.rb', line 89 def put_mapping(type, mapping) params = {} if ignore_conflicts = mapping.delete(:ignore_conflicts) || mapping.delete("ignore_conflicts") params[:ignore_conflicts] = ignore_conflicts end url = "#{self.url}/#{type}/_mapping" url += "?#{params.to_param}" unless params.empty? payload = { type => mapping }.to_json @response = Configuration.client.put url, payload result = MultiJson.decode(@response.body) @response.success? ? result : false ensure curl = %Q|curl -X PUT "#{url}" -d '#{payload}'| logged("PUT MAPPING #{type}", curl) end |
#refresh ⇒ Object
391 392 393 394 395 396 397 |
# File 'lib/tire/index.rb', line 391 def refresh @response = Configuration.client.post "#{url}/_refresh", '' ensure curl = %Q|curl -X POST "#{url}/_refresh"| logged('_refresh', curl) end |
#register_percolator_query(name, options = {}, &block) ⇒ Object
429 430 431 432 433 434 435 436 437 438 |
# File 'lib/tire/index.rb', line 429 def register_percolator_query(name, ={}, &block) [:query] = Search::Query.new(&block).to_hash if block_given? @response = Configuration.client.put "#{Configuration.url}/_percolator/#{@name}/#{name}", MultiJson.encode() MultiJson.decode(@response.body)['ok'] ensure curl = %Q|curl -X PUT "#{Configuration.url}/_percolator/#{@name}/#{name}?pretty" -d '#{MultiJson.encode(, :pretty => Configuration.pretty)}'| logged('_percolator', curl) end |
#reindex(name, options = {}, &block) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/tire/index.rb', line 308 def reindex(name, ={}, &block) new_index = Index.new(name) new_index.create() unless new_index.exists? transform = .delete(:transform) Search::Scan.new(self.name, &block).each do |results| documents = results.map do |document| document = document.to_hash.except(:type, :_index, :_explanation, :_score, :_version, :highlight, :sort) document = transform.call(document) if transform document end new_index.bulk_store documents end end |
#remove(*args) ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/tire/index.rb', line 326 def remove(*args) if args.size > 1 type, document = args type = Utils.escape(type) id = get_id_from_document(document) || document else document = args.pop type = get_type_from_document(document) id = get_id_from_document(document) || document end raise ArgumentError, "Please pass a document ID" unless id url = "#{self.url}/#{type}/#{Utils.escape(id)}" result = Configuration.client.delete url MultiJson.decode(result.body) if result.success? ensure curl = %Q|curl -X DELETE "#{url}"| logged("#{type}/#{id}", curl) end |
#remove_alias(alias_name) ⇒ Object
49 50 51 |
# File 'lib/tire/index.rb', line 49 def remove_alias(alias_name) Alias.find(alias_name) { |a| a.indices.delete @name }.save end |
#retrieve(type, id, options = {}) ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/tire/index.rb', line 347 def retrieve(type, id, ={}) raise ArgumentError, "Please pass a document ID" unless id url = "#{self.url}/#{Utils.escape(type)}/#{Utils.escape(id)}" params = {} params[:routing] = [:routing] if [:routing] params[:fields] = [:fields] if [:fields] params[:preference] = [:preference] if [:preference] params_encoded = params.empty? ? '' : "?#{params.to_param}" @response = Configuration.client.get "#{url}#{params_encoded}" h = MultiJson.decode(@response.body) wrapper = [:wrapper] || Configuration.wrapper if wrapper == Hash then h else return nil if h['exists'] == false document = h['_source'] || h['fields'] || {} document.update('id' => h['_id'], '_type' => h['_type'], '_index' => h['_index'], '_version' => h['_version']) wrapper.new(document) end ensure curl = %Q|curl -X GET "#{url}"| logged("#{type}/#{id}", curl) end |
#settings ⇒ Object
117 118 119 120 |
# File 'lib/tire/index.rb', line 117 def settings @response = Configuration.client.get("#{url}/_settings") MultiJson.decode(@response.body)[@name]['settings'] end |
#store(*args) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/tire/index.rb', line 122 def store(*args) document, = args id = get_id_from_document(document) type = get_type_from_document(document) document = convert_document_to_json(document) ||= {} params = {} if [:percolate] params[:percolate] = [:percolate] params[:percolate] = "*" if params[:percolate] === true end params[:parent] = [:parent] if [:parent] params[:routing] = [:routing] if [:routing] params[:replication] = [:replication] if [:replication] params[:version] = [:version] if [:version] params_encoded = params.empty? ? '' : "?#{params.to_param}" url = id ? "#{self.url}/#{type}/#{Utils.escape(id)}#{params_encoded}" : "#{self.url}/#{type}/#{params_encoded}" @response = Configuration.client.post url, document MultiJson.decode(@response.body) ensure curl = %Q|curl -X POST "#{url}" -d '#{document}'| logged([type, id].join('/'), curl) end |
#unregister_percolator_query(name) ⇒ Object
440 441 442 443 444 445 446 447 |
# File 'lib/tire/index.rb', line 440 def unregister_percolator_query(name) @response = Configuration.client.delete "#{Configuration.url}/_percolator/#{@name}/#{name}" MultiJson.decode(@response.body)['ok'] ensure curl = %Q|curl -X DELETE "#{Configuration.url}/_percolator/#{@name}"| logged('_percolator', curl) end |
#update(type, id, payload = {}, options = {}) ⇒ Object
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/tire/index.rb', line 375 def update(type, id, payload={}, ={}) raise ArgumentError, "Please pass a document type" unless type raise ArgumentError, "Please pass a document ID" unless id raise ArgumentError, "Please pass a script or partial document in the payload hash" unless payload[:script] || payload[:doc] type = Utils.escape(type) url = "#{self.url}/#{type}/#{Utils.escape(id)}/_update" url += "?#{.to_param}" unless .keys.empty? @response = Configuration.client.post url, MultiJson.encode(payload) MultiJson.decode(@response.body) ensure curl = %Q|curl -X POST "#{url}" -d '#{MultiJson.encode(payload, :pretty => Configuration.pretty)}'| logged(id, curl) end |
#url ⇒ Object
13 14 15 |
# File 'lib/tire/index.rb', line 13 def url "#{Configuration.url}/#{@name}" end |