class Elasticity::Search::Results

Constants

DEFAULT_SIZE

Public Class Methods

new(response, body, mapper = nil) click to toggle source
# File lib/elasticity/search.rb, line 301
def initialize(response, body, mapper = nil)
  @response = response
  @body = body
  @documents = if mapper.nil?
    @response["hits"]["hits"]
  else
    @response["hits"]["hits"].map { |hit| mapper.(hit) }
  end
end

Public Instance Methods

aggregations() click to toggle source
# File lib/elasticity/search.rb, line 319
def aggregations
  @response["aggregations"] ||= {}
end
current_page() click to toggle source

for pagination

# File lib/elasticity/search.rb, line 344
def current_page
  return 1 if @body[:from].nil?
  @body[:from] / per_page + 1
end
each(&block) click to toggle source
# File lib/elasticity/search.rb, line 315
def each(&block)
  @documents.each(&block)
end
method_missing(name, *args, &block) click to toggle source
# File lib/elasticity/search.rb, line 311
def method_missing(name, *args, &block)
  @documents.public_send(name, *args, &block)
end
next_page() click to toggle source
# File lib/elasticity/search.rb, line 349
def next_page
  current_page < total_pages ? (current_page + 1) : nil
end
per_page() click to toggle source

for pagination

# File lib/elasticity/search.rb, line 339
def per_page
  @body[:size] || DEFAULT_SIZE
end
previous_page() click to toggle source
# File lib/elasticity/search.rb, line 353
def previous_page
  current_page > 1 ? (current_page - 1) : nil
end
total() click to toggle source
# File lib/elasticity/search.rb, line 323
def total
  res = @response["hits"]["total"]
  if res.is_a?(::Hash)
    res["value"]
  else
    res
  end
end
Also aliased as: total_entries
total_entries()
Alias for: total
total_pages() click to toggle source

for pagination

# File lib/elasticity/search.rb, line 334
def total_pages
  (total.to_f / per_page.to_f).ceil
end