Module: Gummi::DbLayer::Document::ClassMethods
- Defined in:
- lib/gummi/db_layer/document.rb
Overview
–––––––––––––Class Methods –––––––––––––
Instance Method Summary collapse
-
#client ⇒ Object
––––––––––––––––––––––––––– Internal Backend Connection –––––––––––––––––––––––––––.
- #create(*args) ⇒ Object
- #creation_options ⇒ Object
- #default_search_options ⇒ Object
- #delete(*args) ⇒ Object
- #delete_by_query(query) ⇒ Object
- #delete_children_by_query(parent_id, children_query) ⇒ Object
- #document_type(*args) ⇒ Object
-
#exists?(id) ⇒ Boolean
Public: Checks if specified document exists.
- #get(id, parent_id = nil) ⇒ Object (also: #find)
- #hit_to_document(hit) ⇒ Object
-
#hits_to_documents(hits) ⇒ Object
––––––––––––––––––––– Public Conversion API –––––––––––––––––––––.
-
#index(*args) ⇒ Object
–––––––––––––––––––––––––––––– Index and Document definitions ––––––––––––––––––––––––––––––.
-
#new_filtered_search(options = {}) ⇒ Object
––––––––––––––––– Public Search API –––––––––––––––––.
- #new_raw_search(options = {}) ⇒ Object
- #parent(model) ⇒ Object
- #parent_document_type(*args) ⇒ Object
- #parent_id_attribute_name(*args) ⇒ Object
-
#sync_mapping! ⇒ Object
–––––––––––––––– Public Index API ––––––––––––––––.
- #update(id, attributes) ⇒ Object
Instance Method Details
#client ⇒ Object
–––––––––––––––––––––––––––Internal Backend Connection –––––––––––––––––––––––––––
180 181 182 |
# File 'lib/gummi/db_layer/document.rb', line 180 def client Gummi::API.client end |
#create(*args) ⇒ Object
35 36 37 38 |
# File 'lib/gummi/db_layer/document.rb', line 35 def create(*args) instance = new(*args) instance.create ? instance : nil end |
#creation_options ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/gummi/db_layer/document.rb', line 142 def 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 |
#default_search_options ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/gummi/db_layer/document.rb', line 168 def { document_class: self, index: index.name, type: document_type, } end |
#delete(*args) ⇒ Object
56 57 58 59 60 |
# File 'lib/gummi/db_layer/document.rb', line 56 def delete(*args) delete! *args rescue ::Elasticsearch::Transport::Transport::Errors::NotFound nil end |
#delete_by_query(query) ⇒ Object
81 82 83 84 |
# File 'lib/gummi/db_layer/document.rb', line 81 def delete_by_query(query) = { index: index.name, type: document_type, body: query } Hashie::Mash.new client.delete_by_query end |
#delete_children_by_query(parent_id, children_query) ⇒ Object
75 76 77 78 79 |
# File 'lib/gummi/db_layer/document.rb', line 75 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] } } } delete_by_query query end |
#document_type(*args) ⇒ Object
111 112 113 114 |
# File 'lib/gummi/db_layer/document.rb', line 111 def document_type(*args) @document_type = args.first.to_sym unless args.empty? @document_type || name.demodulize.underscore.to_sym end |
#exists?(id) ⇒ Boolean
Public: Checks if specified document exists
id - The document id
Examples
DB::Person.exists?('123')
# => true
Returns true or false
31 32 33 |
# File 'lib/gummi/db_layer/document.rb', line 31 def exists?(id) client.exists index: index.name, type: document_type, id: id end |
#get(id, parent_id = nil) ⇒ Object Also known as: find
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gummi/db_layer/document.rb', line 40 def get(id, parent_id = nil) = { index: index.name, type: document_type, id: id, fields: %w{ _source _parent } } if parent_id .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: do Hashie::Mash.new client.get end hit_to_document response rescue ::Elasticsearch::Transport::Transport::Errors::NotFound nil end |
#hit_to_document(hit) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/gummi/db_layer/document.rb', line 95 def hit_to_document(hit) attributes = { id: hit._id, version: hit._version } attributes.merge!(parent_id_attribute_name => hit.fields._parent) if hit.fields && hit.fields._parent attributes.merge! hit._source if hit._source self.new attributes end |
#hits_to_documents(hits) ⇒ Object
–––––––––––––––––––––Public Conversion API –––––––––––––––––––––
90 91 92 93 |
# File 'lib/gummi/db_layer/document.rb', line 90 def hits_to_documents(hits) documents = [hits].flatten.map { |hit| hit_to_document(hit) } documents.length > 1 ? documents : documents.first end |
#index(*args) ⇒ Object
––––––––––––––––––––––––––––––Index and Document definitions ––––––––––––––––––––––––––––––
106 107 108 109 |
# File 'lib/gummi/db_layer/document.rb', line 106 def index(*args) @index = args.first unless args.empty? @index || Gummi::DbLayer::DefaultIndex end |
#new_filtered_search(options = {}) ⇒ Object
–––––––––––––––––Public Search API –––––––––––––––––
160 161 162 |
# File 'lib/gummi/db_layer/document.rb', line 160 def new_filtered_search( = {}) Gummi::DbLayer::Document::Search::Filtered.new .merge() end |
#new_raw_search(options = {}) ⇒ Object
164 165 166 |
# File 'lib/gummi/db_layer/document.rb', line 164 def new_raw_search( = {}) Gummi::DbLayer::Document::Search::Raw.new .merge() end |
#parent(model) ⇒ Object
116 117 118 |
# File 'lib/gummi/db_layer/document.rb', line 116 def parent(model) parent_document_type model.document_type end |
#parent_document_type(*args) ⇒ Object
120 121 122 123 124 |
# File 'lib/gummi/db_layer/document.rb', line 120 def parent_document_type(*args) @parent_document_type = args.first unless args.empty? parent_id_attribute_name "#{@parent_document_type}_id".to_sym if @parent_document_type && !parent_id_attribute_name @parent_document_type end |
#parent_id_attribute_name(*args) ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/gummi/db_layer/document.rb', line 126 def parent_id_attribute_name(*args) unless args.empty? @parent_id_attribute_name = args.first attr_accessor @parent_id_attribute_name end @parent_id_attribute_name end |
#sync_mapping! ⇒ Object
––––––––––––––––Public Index API ––––––––––––––––
138 139 140 |
# File 'lib/gummi/db_layer/document.rb', line 138 def sync_mapping! client.indices.put_mapping end |
#update(id, attributes) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/gummi/db_layer/document.rb', line 62 def update(id, attributes) return unless id.present? if parent_id_attribute_name && parent_id = self.send(parent_id_attribute_name) = { parent: parent_id } else = {} end client.update .merge(index: index.name, type: document_type, id: id, body: { doc: attributes }) true rescue ::Elasticsearch::Transport::Transport::Errors::NotFound nil end |