Module: Elasticsearch::Persistence::Repository::Store
- Included in:
- Class
- Defined in:
- lib/elasticsearch/persistence/repository/store.rb
Overview
Save and delete documents in Elasticsearch
Instance Method Summary collapse
-
#delete(document, options = {}) ⇒ Hash
Remove the serialized object or document with specified ID from Elasticsearch.
-
#save(document, options = {}) ⇒ Hash
Store the serialized object in Elasticsearch.
-
#update(document, options = {}) ⇒ Hash
Update the serialized object in Elasticsearch with partial data or script.
Instance Method Details
#delete(document, options = {}) ⇒ Hash
Remove the serialized object or document with specified ID from Elasticsearch
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/elasticsearch/persistence/repository/store.rb', line 80 def delete(document, ={}) if document.is_a?(String) || document.is_a?(Integer) id = document type = document_type || __get_type_from_class(klass) else serialized = serialize(document) id = __get_id_from_document(serialized) type = document_type || __get_type_from_class(klass || document.class) end client.delete( { index: index_name, type: type, id: id }.merge() ) end |
#save(document, options = {}) ⇒ Hash
Store the serialized object in Elasticsearch
17 18 19 20 21 22 |
# File 'lib/elasticsearch/persistence/repository/store.rb', line 17 def save(document, ={}) serialized = serialize(document) id = __get_id_from_document(serialized) type = document_type || __get_type_from_class(klass || document.class) client.index( { index: index_name, type: type, id: id, body: serialized }.merge() ) end |
#update(document, options = {}) ⇒ Hash
Update the serialized object in Elasticsearch with partial data or script
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/elasticsearch/persistence/repository/store.rb', line 38 def update(document, ={}) case when document.is_a?(String) || document.is_a?(Integer) id = document when document.respond_to?(:to_hash) serialized = document.to_hash id = __extract_id_from_document(serialized) else raise ArgumentError, "Expected a document ID or a Hash-like object, #{document.class} given" end type = .delete(:type) || \ (defined?(serialized) && serialized && serialized.delete(:type)) || \ document_type || \ __get_type_from_class(klass) if defined?(serialized) && serialized body = if serialized[:script] serialized.select { |k, v| [:script, :params, :upsert].include? k } else { doc: serialized } end else body = {} body.update( doc: .delete(:doc)) if [:doc] body.update( script: .delete(:script)) if [:script] body.update( params: .delete(:params)) if [:params] body.update( upsert: .delete(:upsert)) if [:upsert] end client.update( { index: index_name, type: type, id: id, body: body }.merge() ) end |