Module: Elastics::ActiveRecord::HelperMethods::ClassMethods
- Defined in:
- lib/elastics/active_record/helper_methods.rb
Instance Method Summary collapse
- #bulk_elastics(params = {}, &block) ⇒ Object
- #elastics_mapping ⇒ Object
- #elastics_params ⇒ Object
- #find_all_ordered(ids) ⇒ Object
- #index_all_elastics(*args) ⇒ Object
- #refresh_elastics ⇒ Object
-
#reindex_elastics(options = {}) ⇒ Object
Reindexes records with ‘#index_all_elastics`.
- #request_elastics(params) ⇒ Object
- #search_elastics(data = {}, options = {}) ⇒ Object
Instance Method Details
#bulk_elastics(params = {}, &block) ⇒ Object
23 24 25 |
# File 'lib/elastics/active_record/helper_methods.rb', line 23 def bulk_elastics(params = {}, &block) elastics.bulk(elastics_params.merge!(params), &block) end |
#elastics_mapping ⇒ Object
43 44 45 |
# File 'lib/elastics/active_record/helper_methods.rb', line 43 def elastics_mapping request_elastics(method: :get, id: :_mapping) end |
#elastics_params ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/elastics/active_record/helper_methods.rb', line 11 def elastics_params { index: elastics_index_name, type: elastics_type_name, model: self, } end |
#find_all_ordered(ids) ⇒ Object
38 39 40 41 |
# File 'lib/elastics/active_record/helper_methods.rb', line 38 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
47 48 49 50 51 52 53 54 55 |
# File 'lib/elastics/active_record/helper_methods.rb', line 47 def index_all_elastics(*args) find_in_batches(*args) do |batch| bulk_elastics do |bulk| batch.each do |record| bulk.index record.id, record.to_elastics end end end end |
#refresh_elastics ⇒ Object
75 76 77 |
# File 'lib/elastics/active_record/helper_methods.rb', line 75 def refresh_elastics request_elastics(method: :post, type: nil, id: :_refresh) 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.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/elastics/active_record/helper_methods.rb', line 63 def reindex_elastics( = {}) scope = respond_to?(:reindex_scope) ? reindex_scope : all if after = .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() end |
#request_elastics(params) ⇒ Object
19 20 21 |
# File 'lib/elastics/active_record/helper_methods.rb', line 19 def request_elastics(params) elastics.request(elastics_params.merge!(params)) end |
#search_elastics(data = {}, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/elastics/active_record/helper_methods.rb', line 27 def search_elastics(data = {}, = {}) request = { id: :_search, body: data, } if routing = [:routing] request[:query] = {routing: routing} end SearchResult.new self, request_elastics(request), end |