Module: Elastics::ActiveRecord::HelperMethods::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#elastics_mappingObject



29
30
31
# File 'lib/elastics/active_record/helper_methods.rb', line 29

def elastics_mapping
  request_elastics(method: :get, id: :_mapping)
end

#find_all_ordered(ids) ⇒ Object



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

def find_all_ordered(ids)
  items_by_id = where(id: ids).index_by(&:id)
  ids.map { |i| items_by_id[i] }
end

#index_all_elastics(*args) ⇒ Object



33
34
35
36
37
# File 'lib/elastics/active_record/helper_methods.rb', line 33

def index_all_elastics(*args)
  find_in_batches(*args) do |batch|
    index_batch_elastics(batch)
  end
end

#reindex_elastics(options = {}) ⇒ Object

Reindexes records with ‘#index_all_elastics`. If model has scope named `reindex_scope`, this method will apply it.

Also supports ‘:updated_after` option to reindex only updated records. Nothing is performed when `:updated_after` is set but model has not `updated_at` column.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/elastics/active_record/helper_methods.rb', line 45

def reindex_elastics(options = {})
  scope = respond_to?(:reindex_scope) ? reindex_scope : all
  if after = options.delete(:updated_after)
    if updated_at = arel_table[:updated_at]
      scope = scope.where(updated_at.gt(after))
    else
      return
    end
  end
  scope.index_all_elastics(options)
end

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



13
14
15
16
17
18
19
20
21
22
# File 'lib/elastics/active_record/helper_methods.rb', line 13

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