Class: Elastics::ActiveRecord::SearchResult

Inherits:
Object
  • Object
show all
Defined in:
lib/elastics/active_record/search_result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, result, options = {}) ⇒ SearchResult



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

def initialize(model, result, options = {})
  @model = model
  @result = result
  @options = options
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



4
5
6
# File 'lib/elastics/active_record/search_result.rb', line 4

def result
  @result
end

Instance Method Details

#aggregationsObject



40
41
42
# File 'lib/elastics/active_record/search_result.rb', line 40

def aggregations
  @aggregations ||= @result['aggregations'.freeze]
end

#collectionObject



32
33
34
# File 'lib/elastics/active_record/search_result.rb', line 32

def collection
  @collection ||= @model.find_all_ordered ids_to_find
end

#hitsObject



12
13
14
# File 'lib/elastics/active_record/search_result.rb', line 12

def hits
  @hits ||= @result['hits'.freeze]
end

#idsObject



16
17
18
# File 'lib/elastics/active_record/search_result.rb', line 16

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

#ids_to_findObject



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

def ids_to_find
  @ids_to_find ||= begin
    limit = @options[:limit]
    limit ? ids[0...limit] : ids
  end
end

#relationObject



36
37
38
# File 'lib/elastics/active_record/search_result.rb', line 36

def relation
  @model.where id: ids_to_find
end

#rest_idsObject



27
28
29
30
# File 'lib/elastics/active_record/search_result.rb', line 27

def rest_ids
  limit = @options[:limit]
  limit ? ids[limit..-1] : []
end

#totalObject



44
45
46
# File 'lib/elastics/active_record/search_result.rb', line 44

def total
  hits['total'.freeze]
end