Module: Lycra::Document::Proxy::ClassMethods
- Defined in:
- lib/lycra/document/proxy.rb
Instance Method Summary collapse
- #as_indexed_json(subj, options = {}) ⇒ Object
- #as_json(options = {}) ⇒ Object
- #create_alias(options = {}) ⇒ Object
- #create_alias!(options = {}) ⇒ Object
- #create_index(options = {}) ⇒ Object
- #create_index!(options = {}) ⇒ Object
- #delete(options = {}, &block) ⇒ Object
- #delete!(options = {}, &block) ⇒ Object
- #delete_alias(options = {}) ⇒ Object
- #delete_alias!(options = {}) ⇒ Object
- #delete_index(options = {}) ⇒ Object
- #delete_index!(options = {}) ⇒ Object
- #import(options = {}, &block) ⇒ Object
- #import!(options = {}, &block) ⇒ Object
- #import_scope(scope = nil, &block) ⇒ Object
- #import_scope=(scope) ⇒ Object
- #inherited(child) ⇒ Object
- #inspect ⇒ Object
- #refresh_index(options = {}) ⇒ Object
- #refresh_index!(options = {}) ⇒ Object
- #update(options = {}, &block) ⇒ Object
- #update!(options = {}, &block) ⇒ Object
Instance Method Details
#as_indexed_json(subj, options = {}) ⇒ Object
253 254 255 |
# File 'lib/lycra/document/proxy.rb', line 253 def as_indexed_json(subj, ={}) resolve!(subj).as_json() end |
#as_json(options = {}) ⇒ Object
257 258 259 260 261 262 263 |
# File 'lib/lycra/document/proxy.rb', line 257 def as_json(={}) { index: index_name, document: document_type, subject: subject_type.name } .merge(attributes.map { |k,a| [a.name, a.type.type] }.to_h) .as_json() end |
#create_alias(options = {}) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/lycra/document/proxy.rb', line 69 def create_alias(={}) create_alias!() rescue => e Lycra.configuration.logger.error(e.) return false end |
#create_alias!(options = {}) ⇒ Object
64 65 66 67 |
# File 'lib/lycra/document/proxy.rb', line 64 def create_alias!(={}) raise Lycra::AbstractClassError, "Cannot create aliases using an abstract class" if abstract? __lycra__.create_alias!() end |
#create_index(options = {}) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/lycra/document/proxy.rb', line 82 def create_index(={}) create_index!() rescue => e Lycra.configuration.logger.error(e.) return false end |
#create_index!(options = {}) ⇒ Object
76 77 78 79 80 |
# File 'lib/lycra/document/proxy.rb', line 76 def create_index!(={}) raise Lycra::AbstractClassError, "Cannot create indices using an abstract class" if abstract? __lycra__.create_index!() __lycra__.create_alias!() unless alias_exists? end |
#delete(options = {}, &block) ⇒ Object
246 247 248 249 250 251 |
# File 'lib/lycra/document/proxy.rb', line 246 def delete(={}, &block) delete!(, &block) rescue => e Lycra.configuration.logger.error(e.) return false end |
#delete!(options = {}, &block) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/lycra/document/proxy.rb', line 216 def delete!(={}, &block) raise Lycra::AbstractClassError, "Cannot delete using an abstract class" if abstract? scope = [:scope] || [:query] || import_scope if scope.is_a?(Proc) scope = subject_type.instance_exec(&scope) elsif scope.is_a?(String) || scope.is_a?(Symbol) scope = subject_type.send(scope) elsif scope.nil? scope = subject_type.all end scope.find_in_batches(batch_size: ([:batch_size] || 200)).each do |batch| items = batch.map do |record| { delete: { _index: index_name, _type: document_type, _id: record.id }.stringify_keys }.stringify_keys end deleted = __lycra__.client.bulk(body: items) yield(deleted) if block_given? end return true end |
#delete_alias(options = {}) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/lycra/document/proxy.rb', line 94 def delete_alias(={}) delete_alias!() rescue => e Lycra.configuration.logger.error(e.) return false end |
#delete_alias!(options = {}) ⇒ Object
89 90 91 92 |
# File 'lib/lycra/document/proxy.rb', line 89 def delete_alias!(={}) raise Lycra::AbstractClassError, "Cannot delete aliases using an abstract class" if abstract? __lycra__.delete_alias!() end |
#delete_index(options = {}) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/lycra/document/proxy.rb', line 107 def delete_index(={}) delete_index!() rescue => e Lycra.configuration.logger.error(e.) return false end |
#delete_index!(options = {}) ⇒ Object
101 102 103 104 105 |
# File 'lib/lycra/document/proxy.rb', line 101 def delete_index!(={}) raise Lycra::AbstractClassError, "Cannot delete indices using an abstract class" if abstract? __lycra__.delete_alias!() if alias_exists? __lycra__.delete_index!() end |
#import(options = {}, &block) ⇒ Object
135 136 137 138 139 140 |
# File 'lib/lycra/document/proxy.rb', line 135 def import(={}, &block) import!(, &block) rescue => e Lycra.configuration.logger.error(e.) return false end |
#import!(options = {}, &block) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/lycra/document/proxy.rb', line 126 def import!(={}, &block) raise Lycra::AbstractClassError, "Cannot import using an abstract class" if abstract? [:scope] ||= import_scope if import_scope.is_a?(String) || import_scope.is_a?(Symbol) [:query] ||= import_scope if import_scope.is_a?(Proc) __lycra__.import(, &block) end |
#import_scope(scope = nil, &block) ⇒ Object
54 55 56 57 58 |
# File 'lib/lycra/document/proxy.rb', line 54 def import_scope(scope=nil, &block) @_lycra_import_scope = scope if scope @_lycra_import_scope = block if block_given? @_lycra_import_scope end |
#import_scope=(scope) ⇒ Object
60 61 62 |
# File 'lib/lycra/document/proxy.rb', line 60 def import_scope=(scope) import_scope scope end |
#inherited(child) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/lycra/document/proxy.rb', line 37 def inherited(child) super if defined?(super) # resets the proxy so it gets recreated for the new class child.send :instance_variable_set, :@__lycra__, nil child.send :instance_variable_set, :@_lycra_import_scope, self.import_scope child.class_eval do self.__lycra__.class_eval do include ::Elasticsearch::Model::Importing::ClassMethods include ::Elasticsearch::Model::Adapter.from_class(child).importing_mixin end end Registry.add(child) end |
#inspect ⇒ Object
265 266 267 |
# File 'lib/lycra/document/proxy.rb', line 265 def inspect "#{name}(index: #{index_name}, document: #{document_type}, subject: #{subject_type}, #{attributes.map { |key,attr| "#{attr.name}: #{attr.nested? ? "[#{attr.type.type}]" : attr.type.type}"}.join(', ')})" end |
#refresh_index(options = {}) ⇒ Object
119 120 121 122 123 124 |
# File 'lib/lycra/document/proxy.rb', line 119 def refresh_index(={}) refresh_index!() rescue => e Lycra.configuration.logger.error(e.) return false end |
#refresh_index!(options = {}) ⇒ Object
114 115 116 117 |
# File 'lib/lycra/document/proxy.rb', line 114 def refresh_index!(={}) raise Lycra::AbstractClassError, "Cannot refresh indices using an abstract class" if abstract? __lycra__.refresh_index!() end |
#update(options = {}, &block) ⇒ Object
209 210 211 212 213 214 |
# File 'lib/lycra/document/proxy.rb', line 209 def update(={}, &block) update!(, &block) rescue => e Lycra.configuration.logger.error(e.) return false end |
#update!(options = {}, &block) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/lycra/document/proxy.rb', line 142 def update!(={}, &block) raise Lycra::AbstractClassError, "Cannot update using an abstract class" if abstract? scope = [:scope] || [:query] || import_scope if scope.is_a?(Proc) scope = subject_type.instance_exec(&scope) elsif scope.is_a?(String) || scope.is_a?(Symbol) scope = subject_type.send(scope) elsif scope.nil? scope = subject_type.all end scope.find_in_batches(batch_size: ([:batch_size] || 200)).each do |batch| = .select { |k,v| [:only,:except].include?(k) } items = batch.map do |record| { update: { _index: index_name, _type: document_type, _id: record.id, data: { doc: new(record).resolve!() }.stringify_keys }.stringify_keys }.stringify_keys end updated = __lycra__.client.bulk(body: items) missing = updated['items'].map do |miss| if miss['update'].key?('error') && miss['update']['error']['type'] == 'document_missing_exception' update = miss['update'] item = items.find { |i| i['update']['_id'].to_s == miss['update']['_id'] }['update'] if .empty? data = item['data']['doc'] else data = new(subject_type.find(update['_id'])).resolve! end { index: { _index: update['_index'], _type: update['_type'], _id: update['_id'], data: data }.stringify_keys }.stringify_keys else nil end end.compact if missing.count > 0 indexed = __lycra__.client.bulk body: missing updated['items'] = updated['items'].map do |item| miss = indexed['items'].find { |i| i['index']['_id'] == item['update']['_id'] } miss || item end end yield(updated) if block_given? end return true end |