Module: Lycra::Document::Proxy::ClassMethods

Defined in:
lib/lycra/document/proxy.rb

Instance Method Summary collapse

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, options={})
  resolve!(subj).as_json(options)
end

#as_json(options = {}) ⇒ Object



257
258
259
260
261
262
263
# File 'lib/lycra/document/proxy.rb', line 257

def as_json(options={})
  { index: index_name,
    document: document_type,
    subject: subject_type.name }
    .merge(attributes.map { |k,a| [a.name, a.type.type] }.to_h)
    .as_json(options)
end

#create_alias(options = {}) ⇒ Object



69
70
71
72
73
74
# File 'lib/lycra/document/proxy.rb', line 69

def create_alias(options={})
  create_alias!(options)
rescue => e
  Lycra.configuration.logger.error(e.message)
  return false
end

#create_alias!(options = {}) ⇒ Object



64
65
66
67
# File 'lib/lycra/document/proxy.rb', line 64

def create_alias!(options={})
  raise Lycra::AbstractClassError, "Cannot create aliases using an abstract class" if abstract?
  __lycra__.create_alias!(options)
end

#create_index(options = {}) ⇒ Object



82
83
84
85
86
87
# File 'lib/lycra/document/proxy.rb', line 82

def create_index(options={})
  create_index!(options)
rescue => e
  Lycra.configuration.logger.error(e.message)
  return false
end

#create_index!(options = {}) ⇒ Object



76
77
78
79
80
# File 'lib/lycra/document/proxy.rb', line 76

def create_index!(options={})
  raise Lycra::AbstractClassError, "Cannot create indices using an abstract class" if abstract?
  __lycra__.create_index!(options)
  __lycra__.create_alias!(options) unless alias_exists?
end

#delete(options = {}, &block) ⇒ Object



246
247
248
249
250
251
# File 'lib/lycra/document/proxy.rb', line 246

def delete(options={}, &block)
  delete!(options, &block)
rescue => e
  Lycra.configuration.logger.error(e.message)
  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!(options={}, &block)
  raise Lycra::AbstractClassError, "Cannot delete using an abstract class" if abstract?

  scope = options[:scope] || options[: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: (options[: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(options={})
  delete_alias!(options)
rescue => e
  Lycra.configuration.logger.error(e.message)
  return false
end

#delete_alias!(options = {}) ⇒ Object



89
90
91
92
# File 'lib/lycra/document/proxy.rb', line 89

def delete_alias!(options={})
  raise Lycra::AbstractClassError, "Cannot delete aliases using an abstract class" if abstract?
  __lycra__.delete_alias!(options)
end

#delete_index(options = {}) ⇒ Object



107
108
109
110
111
112
# File 'lib/lycra/document/proxy.rb', line 107

def delete_index(options={})
  delete_index!(options)
rescue => e
  Lycra.configuration.logger.error(e.message)
  return false
end

#delete_index!(options = {}) ⇒ Object



101
102
103
104
105
# File 'lib/lycra/document/proxy.rb', line 101

def delete_index!(options={})
  raise Lycra::AbstractClassError, "Cannot delete indices using an abstract class" if abstract?
  __lycra__.delete_alias!(options) if alias_exists?
  __lycra__.delete_index!(options)
end

#import(options = {}, &block) ⇒ Object



135
136
137
138
139
140
# File 'lib/lycra/document/proxy.rb', line 135

def import(options={}, &block)
  import!(options, &block)
rescue => e
  Lycra.configuration.logger.error(e.message)
  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!(options={}, &block)
  raise Lycra::AbstractClassError, "Cannot import using an abstract class" if abstract?

  options[:scope] ||= import_scope if import_scope.is_a?(String) || import_scope.is_a?(Symbol)
  options[:query] ||= import_scope if import_scope.is_a?(Proc)

  __lycra__.import(options, &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

#inspectObject



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(options={})
  refresh_index!(options)
rescue => e
  Lycra.configuration.logger.error(e.message)
  return false
end

#refresh_index!(options = {}) ⇒ Object



114
115
116
117
# File 'lib/lycra/document/proxy.rb', line 114

def refresh_index!(options={})
  raise Lycra::AbstractClassError, "Cannot refresh indices using an abstract class" if abstract?
  __lycra__.refresh_index!(options)
end

#update(options = {}, &block) ⇒ Object



209
210
211
212
213
214
# File 'lib/lycra/document/proxy.rb', line 209

def update(options={}, &block)
  update!(options, &block)
rescue => e
  Lycra.configuration.logger.error(e.message)
  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!(options={}, &block)
  raise Lycra::AbstractClassError, "Cannot update using an abstract class" if abstract?

  scope = options[:scope] || options[: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: (options[:batch_size] || 200)).each do |batch|
    json_options = options.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!(json_options)
          }.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 json_options.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