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

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

Instance Method Summary collapse

Instance Method Details

#find_all_ordered(ids, conditions_present = false) ⇒ Object

Finds items by ids and returns array in the order in which ids were given. Every missing record is replaced with nil in the result. If conditions_present is true it doesn’t add where clause.



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

def find_all_ordered(ids, conditions_present = false)
  relation = conditions_present ? where(id: ids) : self
  items_by_id = relation.index_by(&:id)
  ids.map { |i| items_by_id[i] }
end

#index_all_elastics(*args) ⇒ Object

Indexes all records in current scope.



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

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.



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

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

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



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

def search_elastics(data = {}, options = {})
  options[:result_class] ||= SearchResult
  options[:model] = self
  super
end