lib/gummi/document.rb in gummi-0.0.6 vs lib/gummi/document.rb in gummi-0.0.7

- old
+ new

@@ -2,11 +2,11 @@ module Document extend ActiveSupport::Concern included do include Virtus.model - include Gummi::Attributes + include Gummi::Document::Attributes end attr_accessor :id attr_accessor :version @@ -19,10 +19,21 @@ else false end end + def create + response = client.create index: index.name, type: document_type, id: id, body: attributes + if response["ok"] + self.version = response["_version"] + self.id = response["_id"] + true + else + false + end + end + def update response = client.update index: index.name, type: document_type, id: id, retry_on_conflict: 0, version: version, body: { doc: attributes.as_json } if response["ok"] self.version = response["_version"] true @@ -45,10 +56,20 @@ self.class.index end module ClassMethods + def create(attributes) + document = self.new(attributes) + response = client.create index: index.name, type: document_type, id: document.id, body: document.attributes + if response["ok"] + document.version = response["_version"] + document.id = response["_id"] + document + end + end + def get!(id) response = client.get index: index.name, type: document_type, id: id doc_hash = {id: response["_id"], version: response["_version"]}.merge(response["_source"]) lot = self.new(doc_hash) lot.version = response["_version"] @@ -89,10 +110,10 @@ args = {} args[:index] = index.name args[:type] = document_type args.merge! options - Gummi::Search::Filtered.new args + Gummi::Document::Search::Filtered.new args end def creation_options result = { index: index.name, \ No newline at end of file