Class: Elastic::SiteSearch::ResultSet

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic/site-search/result_set.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results) ⇒ ResultSet

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



20
21
22
23
24
# File 'lib/elastic/site-search/result_set.rb', line 20

def initialize(results)
  @records = results['records']
  @info = results['info']
  @errors = results['errors']
end

Instance Attribute Details

#errorsObject (readonly)

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



8
9
10
# File 'lib/elastic/site-search/result_set.rb', line 8

def errors
  @errors
end

#infoObject (readonly)

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.



17
18
19
# File 'lib/elastic/site-search/result_set.rb', line 17

def info
  @info
end

#recordsObject (readonly)

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



12
13
14
# File 'lib/elastic/site-search/result_set.rb', line 12

def records
  @records
end

Instance Method Details

#[](document_type) ⇒ Object

Get results for the provided DocumentType

Parameters:

  • document_type (String)

    the DocumentType slug to get results for



29
30
31
# File 'lib/elastic/site-search/result_set.rb', line 29

def [](document_type)
  records[document_type]
end

#current_pageObject

Return the page of results for this ResultSet



48
49
50
# File 'lib/elastic/site-search/result_set.rb', line 48

def current_page
  info[info.keys.first]['current_page']
end

#document_typesObject

Return a list of DocumentType slugs represented in the ResultSet.



34
35
36
# File 'lib/elastic/site-search/result_set.rb', line 34

def document_types
  records.keys
end

#facets(document_type) ⇒ Object

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

Parameters:

  • document_type (String)

    the DocumentType slug to get facets for



43
44
45
# File 'lib/elastic/site-search/result_set.rb', line 43

def facets(document_type)
  info[document_type]['facets']
end

#num_pages(document_type = nil) ⇒ Object

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.

Parameters:

  • document_type (String) (defaults to: nil)

    the DocumentType slug to return the number of pages for



65
66
67
68
69
70
71
# 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_pageObject

Return the number of results per page.



53
54
55
# File 'lib/elastic/site-search/result_set.rb', line 53

def per_page
  info[info.keys.first]['per_page']
end

#queryObject

Return the query used for this search



79
80
81
# File 'lib/elastic/site-search/result_set.rb', line 79

def query
  info[info.keys.first]['query']
end

#total_result_count(document_type) ⇒ Object

Return the total number of results for the query



74
75
76
# File 'lib/elastic/site-search/result_set.rb', line 74

def total_result_count(document_type)
  info[document_type]['total_result_count']
end