class EagleSearch::Response

Public Class Methods

new(klass, response, options) click to toggle source
# File lib/eagle_search/response.rb, line 3
def initialize(klass, response, options)
  @klass    = klass
  @response = response
  @options  = options
end

Public Instance Methods

aggregations() click to toggle source
# File lib/eagle_search/response.rb, line 35
def aggregations
  @response["aggregations"]
end
current_page() click to toggle source
# File lib/eagle_search/response.rb, line 39
def current_page
  @options[:page] || 1
end
each() { |e| ... } click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/eagle_search/response.rb, line 47
def limit_value
  @options[:per_page] || 25
end
records() click to toggle source
# 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() click to toggle source
# File lib/eagle_search/response.rb, line 24
def total_hits
  @response["hits"]["total"]
end
total_pages() click to toggle source
# File lib/eagle_search/response.rb, line 43
def total_pages
  (total_hits / limit_value.to_f).ceil
end