Module: Elasticsearch::Model::MongoidExtensions::SCI::ClassMethods

Defined in:
lib/elasticsearch/model/mongoid_extensions/sci.rb

Instance Method Summary collapse

Instance Method Details

#create_index!(options = {}) ⇒ Object



44
45
46
47
48
# File 'lib/elasticsearch/model/mongoid_extensions/sci.rb', line 44

def create_index!(options = {})
  res = Array(__elasticsearch__.create_index!(options))
  descendants.each { |cls| res << cls.__elasticsearch__.create_index!(options) }
  res.compact
end

#document_type_template(proc) ⇒ Object



17
18
19
20
# File 'lib/elasticsearch/model/mongoid_extensions/sci.rb', line 17

def document_type_template(proc)
  @@document_type_template = proc
  document_type @@document_type_template.call(self)
end

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



61
62
63
64
# File 'lib/elasticsearch/model/mongoid_extensions/sci.rb', line 61

def import(options = {}, &block)
  __elasticsearch__.import(options.merge(criteria: criteria.type(to_s)), &block).to_i +
    subclasses.sort_by(&:model_name).map { |cls| cls.import(options, &block) }.sum
end

#index_name_template(proc) ⇒ Object



12
13
14
15
# File 'lib/elasticsearch/model/mongoid_extensions/sci.rb', line 12

def index_name_template(proc)
  @@index_name_template = proc
  index_name @@index_name_template.call(self)
end

#inherited(descendant) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/elasticsearch/model/mongoid_extensions/sci.rb', line 22

def inherited(descendant)
  super(descendant)

  descendant.instance_eval do
    include Elasticsearch::Model
  end

  # propagate index_name_template
  descendant.index_name @@index_name_template.call(descendant) if defined?(@@index_name_template)
  descendant.document_type @@document_type_template.call(descendant) if defined?(@@document_type_template)

  # propagate settings
  descendant.settings settings.to_hash

  # propagate mapping to all descendants
  mapping.instance_variable_get(:@mapping).each do |name, options|
    descendant.mapping do
      indexes(name, options)
    end
  end
end

#refresh_index!(options = {}) ⇒ Object



50
51
52
53
54
# File 'lib/elasticsearch/model/mongoid_extensions/sci.rb', line 50

def refresh_index!(options = {})
  res = Array(__elasticsearch__.refresh_index!(options))
  descendants.each { |cls| res << cls.__elasticsearch__.refresh_index!(options) }
  res.compact
end

#search(query_or_payload, options = {}) ⇒ Object



56
57
58
59
# File 'lib/elasticsearch/model/mongoid_extensions/sci.rb', line 56

def search(query_or_payload, options = {})
  models = options.fetch :models, [self] + descendants
  Elasticsearch::Model.search(query_or_payload, models, options.except(:models))
end