Class: Elastics::ActiveRecord::SearchResult

Inherits:
Result::Search show all
Defined in:
lib/elastics/active_record/search_result.rb

Instance Attribute Summary

Attributes inherited from Result::Search

#response

Instance Method Summary collapse

Methods inherited from Result::Search

#aggregations, #hits, #ids_to_find, #rest_ids, #total

Constructor Details

#initialize(response, options = {}) ⇒ SearchResult

It expects ‘:model` option with a model-class. Optionally pass `scope` option with a lambda which takes and modifies relation.



7
8
9
10
11
# File 'lib/elastics/active_record/search_result.rb', line 7

def initialize(response, options = {})
  @model = options[:model]
  @scope = options[:scope]
  super response, options
end

Instance Method Details

#collectionObject



18
19
20
# File 'lib/elastics/active_record/search_result.rb', line 18

def collection
  @collection ||= relation.find_all_ordered(ids_to_find, true)
end

#idsObject

super.map(&:to_i)



14
15
16
# File 'lib/elastics/active_record/search_result.rb', line 14

def ids
  @ids ||= hits['hits'.freeze].map { |x| x['_id'.freeze].to_i }
end

#relationObject



22
23
24
25
26
27
# File 'lib/elastics/active_record/search_result.rb', line 22

def relation
  @relation ||= begin
    result = @model.where(id: ids_to_find)
    @scope ? @scope.call(result) : result
  end
end