Module: Elasticsearch::Model::Adapter::ActiveRecord::Records
- Defined in:
- lib/elasticsearch/model/adapters/active_record.rb
Instance Method Summary collapse
-
#load ⇒ Object
Prevent clash with ‘ActiveSupport::Dependencies::Loadable`.
-
#order(*args) ⇒ Object
Intercept call to the ‘order` method, so we can ignore the order from Elasticsearch.
-
#records ⇒ Object
Returns an ‘ActiveRecord::Relation` instance.
Instance Method Details
#load ⇒ Object
Prevent clash with ‘ActiveSupport::Dependencies::Loadable`
37 38 39 |
# File 'lib/elasticsearch/model/adapters/active_record.rb', line 37 def load records.load end |
#order(*args) ⇒ Object
Intercept call to the ‘order` method, so we can ignore the order from Elasticsearch
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/elasticsearch/model/adapters/active_record.rb', line 43 def order(*args) sql_records = records.__send__ :order, *args # Redefine the `to_a` method to the original one # sql_records.instance_exec do define_singleton_method(:to_a) do if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4 self.load else self.__send__(:exec_queries) end @records end end sql_records end |
#records ⇒ Object
Returns an ‘ActiveRecord::Relation` instance
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/elasticsearch/model/adapters/active_record.rb', line 15 def records sql_records = klass.where(klass.primary_key => ids) # Re-order records based on the order from Elasticsearch hits # by redefining `to_a`, unless the user has called `order()` # sql_records.instance_exec(response.response['hits']['hits']) do |hits| define_singleton_method :to_a do if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4 self.load else self.__send__(:exec_queries) end @records.sort_by { |record| hits.index { |hit| hit['_id'].to_s == record.id.to_s } } end end sql_records end |