Class: EagleSearch::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/eagle_search/response.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, response, options) ⇒ Response

Returns a new instance of Response.



3
4
5
6
7
# File 'lib/eagle_search/response.rb', line 3

def initialize(klass, response, options)
  @klass    = klass
  @response = response
  @options  = options
end

Instance Method Details

#aggregationsObject



35
36
37
# File 'lib/eagle_search/response.rb', line 35

def aggregations
  @response["aggregations"]
end

#current_pageObject



39
40
41
# File 'lib/eagle_search/response.rb', line 39

def current_page
  @options[:page] || 1
end

#eachObject



16
17
18
19
20
21
22
# File 'lib/eagle_search/response.rb', line 16

def each
  if block_given?
    records.each { |e| yield(e) }
  else
    records.to_enum
  end
end

#hitsObject



28
29
30
31
32
33
# File 'lib/eagle_search/response.rb', line 28

def hits
  @response["hits"]["hits"].each_with_index do |h, index|
    @response["hits"]["hits"][index]["highlight"] = Hash[h["highlight"].map { |field, value| [field, value.first] }] if @response["hits"]["hits"][index]["highlight"]
  end if @response["hits"]["hits"]
  @response["hits"]["hits"]
end

#limit_valueObject



47
48
49
# File 'lib/eagle_search/response.rb', line 47

def limit_value
  @options[:per_page] || 25
end

#recordsObject



9
10
11
12
13
14
# File 'lib/eagle_search/response.rb', line 9

def records
  ids = hits.map { |hit| hit["_id"] }
  #avoids n+1
  @klass.includes(@options[:includes]) if @options[:includes]
  @klass.where(@klass.primary_key => ids)
end

#total_hitsObject



24
25
26
# File 'lib/eagle_search/response.rb', line 24

def total_hits
  @response["hits"]["total"]
end

#total_pagesObject



43
44
45
# File 'lib/eagle_search/response.rb', line 43

def total_pages
  (total_hits / limit_value.to_f).ceil
end