lib/gummi/db_layer/document.rb in gummi-1.0.0 vs lib/gummi/db_layer/document.rb in gummi-1.0.2

- old
+ new

@@ -16,24 +16,44 @@ # –––––––––––––––––––––– # Public Persistence API # –––––––––––––––––––––– + # Public: Checks if specified document exists + # + # id - The document id + # + # Examples + # + # DB::Person.exists?('123') + # # => true + # + # Returns true or false def exists?(id) client.exists index: index.name, type: document_type, id: id end def create(*args) instance = new(*args) instance.create ? instance : nil end - def get(*args) - get! *args + def get(id, parent_id = nil) + options = { index: index.name, type: document_type, id: id, fields: %w{ _source _parent } } + if parent_id + options.merge! parent: parent_id + elsif parent_document_type + raise ArgumentError, "The parent_id attribute is required for getting #{name} from Elastic Search" + end + response = ActiveSupport::Notifications.instrument "search.elasticsearch", name: "Document#get", search: options do + Hashie::Mash.new client.get options + end + hit_to_document response rescue ::Elasticsearch::Transport::Transport::Errors::NotFound nil end + alias :find :get def delete(*args) delete! *args rescue ::Elasticsearch::Transport::Transport::Errors::NotFound nil @@ -44,12 +64,14 @@ if parent_id_attribute_name && parent_id = self.send(parent_id_attribute_name) options = { parent: parent_id } else options = {} end - response = Hashie::Mash.new client.update options.merge(index: index.name, type: document_type, id: id, body: { doc: attributes }) - response.ok + client.update options.merge(index: index.name, type: document_type, id: id, body: { doc: attributes }) + true + rescue ::Elasticsearch::Transport::Transport::Errors::NotFound + nil end def delete_children_by_query(parent_id, children_query) parent_id_query = { term: { _parent: parent_id } } query = { query: { bool: { must: [parent_id_query, children_query] } } } @@ -163,22 +185,10 @@ # –––––––––––––––––––––––– # Internal Persistence API # –––––––––––––––––––––––– - def get!(id, parent_id = nil) - options = { index: index.name, type: document_type, id: id, fields: %w{ _source _parent } } - if parent_id - options.merge! parent: parent_id - elsif parent_document_type - raise ArgumentError, "The parent_id attribute is required for getting #{name} from Elastic Search" - end - response = ActiveSupport::Notifications.instrument "search.elasticsearch", name: "Document#get!", search: options do - Hashie::Mash.new client.get options - end - hit_to_document response - end def delete!(id, parent_id = nil) options = { index: index.name, type: document_type, id: id } if parent_id options.merge! parent: parent_id @@ -186,10 +196,10 @@ raise ArgumentError, "The parent_id attribute is required for getting #{name} from Elastic Search" end response = ActiveSupport::Notifications.instrument "search.elasticsearch", name: "Document#delete!", search: options do Hashie::Mash.new client.delete options end - response.ok && response.found + response.found end end # ––––––––––––––––