module Elasticsearch::Model::Adapter::ActiveRecord::Records

Attributes

options[W]

Public Instance Methods

load() click to toggle source

Prevent clash with `ActiveSupport::Dependencies::Loadable`

# File lib/elasticsearch/model/adapters/active_record.rb, line 68
def load
  records.__send__(:load)
end
options() click to toggle source
# File lib/elasticsearch/model/adapters/active_record.rb, line 32
def options
  @options ||= {}
end
records() click to toggle source

Returns an `ActiveRecord::Relation` instance

# File lib/elasticsearch/model/adapters/active_record.rb, line 38
def records
  sql_records = klass.where(klass.primary_key => ids)
  sql_records = sql_records.includes(self.options[:includes]) if self.options[:includes]

  # 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|
    ar_records_method_name = :to_a
    ar_records_method_name = :records if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 5

    define_singleton_method(ar_records_method_name) do
      if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
        self.load
      else
        self.__send__(:exec_queries)
      end
      if !self.order_values.present?
        @records.sort_by { |record| hits.index { |hit| hit['_id'].to_s == record.id.to_s } }
      else
        @records
      end
    end if self
  end

  sql_records
end