Module: Elastics::Model::HelperMethods::ClassMethods

Defined in:
lib/elastics/model/helper_methods.rb

Instance Method Summary collapse

Instance Method Details

#bulk_elastics(params = {}, &block) ⇒ Object

Proxies #bulk method to elastics client with specified index & type.



30
31
32
# File 'lib/elastics/model/helper_methods.rb', line 30

def bulk_elastics(params = {}, &block)
  elastics.bulk(elastics_params.merge!(params), &block)
end

#clear_elasticsObject

Deletes all records in type keeping its mapping using “Delete by query” API.



71
72
73
# File 'lib/elastics/model/helper_methods.rb', line 71

def clear_elastics
  request_elastics method: :delete, id: :_query, body: {query: {match_all: {}}}
end

#elastics_mappingObject



75
76
77
# File 'lib/elastics/model/helper_methods.rb', line 75

def elastics_mapping
  request_elastics(id: :_mapping)
end

#elastics_paramsObject



16
17
18
19
20
21
22
# File 'lib/elastics/model/helper_methods.rb', line 16

def elastics_params
  {
    index:  elastics_index_name,
    type:   elastics_type_name,
    model:  self,
  }
end

#index_batch_elastics(batch) ⇒ Object

Indexes given records using batch API.



55
56
57
58
59
60
61
# File 'lib/elastics/model/helper_methods.rb', line 55

def index_batch_elastics(batch)
  bulk_elastics do |bulk|
    batch.each do |record|
      bulk.index record.id, record.to_elastics
    end
  end
end

#refresh_elasticsObject

Performs ‘_refresh` request on index.



50
51
52
# File 'lib/elastics/model/helper_methods.rb', line 50

def refresh_elastics
  request_elastics(method: :post, type: nil, id: :_refresh)
end

#reindex_elastics(options = {}) ⇒ Object

Reindexes all records. It requires #find_in_batches method to be defined.



64
65
66
67
68
# File 'lib/elastics/model/helper_methods.rb', line 64

def reindex_elastics(options = {})
  find_in_batches(options) do |batch|
    index_batch_elastics(batch)
  end
end

#request_elastics(params) ⇒ Object

Proxies #request method to elastics client with specified index & type.



25
26
27
# File 'lib/elastics/model/helper_methods.rb', line 25

def request_elastics(params)
  elastics.request(elastics_params.merge!(params))
end

#search_elastics(data = {}, options = {}) ⇒ Object

Performs ‘_search` request on type and instantiates result object. Result::Search is a default result class. It can be overriden with :result_class option.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/elastics/model/helper_methods.rb', line 37

def search_elastics(data = {}, options = {})
  request = {
    id:     :_search,
    body:   data,
  }
  if routing = options[:routing]
    request[:query] = {routing: routing}
  end
  result_class = options[:result_class] || Result::Search
  result_class.new request_elastics(request), options
end