Class: EagleSearch::Response
- Inherits:
-
Object
- Object
- EagleSearch::Response
- Defined in:
- lib/eagle_search/response.rb
Instance Method Summary collapse
- #aggregations ⇒ Object
- #current_page ⇒ Object
- #each ⇒ Object
- #hits ⇒ Object
-
#initialize(klass, response, options) ⇒ Response
constructor
A new instance of Response.
- #limit_value ⇒ Object
- #records ⇒ Object
- #total_hits ⇒ Object
- #total_pages ⇒ Object
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, ) @klass = klass @response = response @options = end |
Instance Method Details
#aggregations ⇒ Object
35 36 37 |
# File 'lib/eagle_search/response.rb', line 35 def aggregations @response["aggregations"] end |
#current_page ⇒ Object
39 40 41 |
# File 'lib/eagle_search/response.rb', line 39 def current_page @options[:page] || 1 end |
#each ⇒ Object
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 |
#hits ⇒ Object
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_value ⇒ Object
47 48 49 |
# File 'lib/eagle_search/response.rb', line 47 def limit_value @options[:per_page] || 25 end |
#records ⇒ Object
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_hits ⇒ Object
24 25 26 |
# File 'lib/eagle_search/response.rb', line 24 def total_hits @response["hits"]["total"] end |
#total_pages ⇒ Object
43 44 45 |
# File 'lib/eagle_search/response.rb', line 43 def total_pages (total_hits / limit_value.to_f).ceil end |