module Gummi module Document extend ActiveSupport::Concern included do include Virtus.model include Gummi::Document::Attributes end attr_accessor :id attr_accessor :version def overwrite response = client.index 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 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 else false end end private def client Gummi::API.client end def document_type self.class.document_type end def index 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"] lot end def get(id) get!(id) rescue ::Elasticsearch::Transport::Transport::Errors::NotFound nil end def document_type(type_name) @document_type = type_name end def document_type @document_type || name.split('::').last.underscore end def index @index || Gummi::DefaultIndex end def index=(index) @index = index end def parent_document_type nil end def sync_mapping! client.indices.put_mapping creation_options end def new_filtered_search(options = {}) args = {} args[:index] = index.name args[:type] = document_type args.merge! options Gummi::Document::Search::Filtered.new args end def creation_options result = { index: index.name, type: document_type, body: { document_type => { properties: mapping, } } } result[:body][document_type].merge!(_parent: { type: parent_document_type }) if parent_document_type.present? result end private def client Gummi::API.client end end end end