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(id, parent_id: nil) ⇒ 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)
Public: Get document using the (realtime) get api.
- #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
- #with_options(options = {}) ⇒ Object
Instance Method Details
#client ⇒ Object
–––––––––––––––––––––––––––Internal Backend Connection –––––––––––––––––––––––––––
197 198 199 |
# File 'lib/gummi/db_layer/document.rb', line 197 def client Gummi.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
159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/gummi/db_layer/document.rb', line 159 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
185 186 187 188 189 190 191 |
# File 'lib/gummi/db_layer/document.rb', line 185 def { document_class: self, index: index.name, type: document_type, } end |
#delete(id, parent_id: nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/gummi/db_layer/document.rb', line 64 def delete(id, parent_id: nil) raise ArgumentError, "parent_id is required for deleting child documents" if parent_document_type && parent_id.blank? = (id: id) .merge! parent: parent_id if parent_id response = ActiveSupport::Notifications.instrument "search.elasticsearch", name: "Document#delete!", search: do Hashie::Mash.new client.delete end rescue ::Elasticsearch::Transport::Transport::Errors::NotFound nil end |
#delete_by_query(query) ⇒ Object
94 95 96 97 |
# File 'lib/gummi/db_layer/document.rb', line 94 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
88 89 90 91 92 |
# File 'lib/gummi/db_layer/document.rb', line 88 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
128 129 130 131 |
# File 'lib/gummi/db_layer/document.rb', line 128 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 (id: id) end |
#get(id, parent_id: nil) ⇒ Object Also known as: find
Public: Get document using the (realtime) get api
id - The document id
parent_id - The parent id (required if getting a child document)
Examples
DB::Rating.get('123', parent_id: '456')
Returns the document if found, nil if not found
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/gummi/db_layer/document.rb', line 51 def get(id, parent_id: nil) raise ArgumentError, "parent_id is required for getting child documents" if parent_document_type && parent_id.blank? = ({ id: id, fields: %w{ _source _parent } }) .merge!(parent: parent_id) if parent_id 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
112 113 114 115 116 117 |
# File 'lib/gummi/db_layer/document.rb', line 112 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 –––––––––––––––––––––
107 108 109 110 |
# File 'lib/gummi/db_layer/document.rb', line 107 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 ––––––––––––––––––––––––––––––
123 124 125 126 |
# File 'lib/gummi/db_layer/document.rb', line 123 def index(*args) @index = args.first unless args.empty? @index || Gummi::DbLayer::DefaultIndex end |
#new_filtered_search(options = {}) ⇒ Object
–––––––––––––––––Public Search API –––––––––––––––––
177 178 179 |
# File 'lib/gummi/db_layer/document.rb', line 177 def new_filtered_search( = {}) Gummi::DbLayer::Document::Search::Filtered.new .merge() end |
#new_raw_search(options = {}) ⇒ Object
181 182 183 |
# File 'lib/gummi/db_layer/document.rb', line 181 def new_raw_search( = {}) Gummi::DbLayer::Document::Search::Raw.new .merge() end |
#parent(model) ⇒ Object
133 134 135 |
# File 'lib/gummi/db_layer/document.rb', line 133 def parent(model) parent_document_type model.document_type end |
#parent_document_type(*args) ⇒ Object
137 138 139 140 141 |
# File 'lib/gummi/db_layer/document.rb', line 137 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
143 144 145 146 147 148 149 |
# File 'lib/gummi/db_layer/document.rb', line 143 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 ––––––––––––––––
155 156 157 |
# File 'lib/gummi/db_layer/document.rb', line 155 def sync_mapping! client.indices.put_mapping end |
#update(id, attributes) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/gummi/db_layer/document.rb', line 75 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 |
#with_options(options = {}) ⇒ Object
99 100 101 |
# File 'lib/gummi/db_layer/document.rb', line 99 def ( = {}) {index: index.name, type: document_type}.merge end |