class Elastic::SiteSearch::ResultSet

The Elastic::SiteSearch::ResultSet class represents a search or suggest result returned by the Elastic::SiteSearch API.

Attributes

errors[R]

@attribute errors [r] a hash of errors for the search (for example filtering on a missing attribute) keyed by DocumentType slug

info[R]

@attribute info [r] a hash of extra query info (for example, facets and number of results) keyed by DocumentType slug. Use the convenience methods of this class for easier access.

records[R]

@attribute records [r] a hash of results for the search keyed by DocumentType slug. Use `[]` to access results more easily.

Public Class Methods

new(results) click to toggle source

Create a Elastic::SiteSearch::ResultSet from deserialized JSON.

# File lib/elastic/site-search/result_set.rb, line 20
def initialize(results)
  @records = results['records']
  @info = results['info']
  @errors = results['errors']
end

Public Instance Methods

[](document_type) click to toggle source

Get results for the provided DocumentType

@param [String] document_type the DocumentType slug to get results for

# File lib/elastic/site-search/result_set.rb, line 29
def [](document_type)
  records[document_type]
end
current_page() click to toggle source

Return the page of results for this ResultSet

# File lib/elastic/site-search/result_set.rb, line 48
def current_page
  info[info.keys.first]['current_page']
end
document_types() click to toggle source

Return a list of DocumentType slugs represented in the ResultSet.

# File lib/elastic/site-search/result_set.rb, line 34
def document_types
  records.keys
end
facets(document_type) click to toggle source

Return the search facets for the provided DocumentType. Will be empty unless a facets parameter was provided when calling the search API.

@param [String] document_type the DocumentType slug to get facets for

# File lib/elastic/site-search/result_set.rb, line 43
def facets(document_type)
  info[document_type]['facets']
end
num_pages(document_type=nil) click to toggle source

Return the number of pages. Since a search can cover multiple DocumentTypes with different numbers of results, the number of pages can vary between DocumentTypes. With no argument, it returns the maximum num_pages for all DocumentTypes in this ResultSet. With a DocumentType slug, it returns the number of pages for that DocumentType.

@param [String] document_type the DocumentType slug to return the number of pages for

# File lib/elastic/site-search/result_set.rb, line 65
def num_pages(document_type=nil)
  if document_type
    info[document_type]['num_pages']
  else
    info.values.map { |v| v['num_pages'] }.max
  end
end
per_page() click to toggle source

Return the number of results per page.

# File lib/elastic/site-search/result_set.rb, line 53
def per_page
  info[info.keys.first]['per_page']
end
query() click to toggle source

Return the query used for this search

# File lib/elastic/site-search/result_set.rb, line 79
def query
  info[info.keys.first]['query']
end
total_result_count(document_type) click to toggle source

Return the total number of results for the query

# File lib/elastic/site-search/result_set.rb, line 74
def total_result_count(document_type)
  info[document_type]['total_result_count']
end