Class: Elastic::SiteSearch::ResultSet
- Inherits:
-
Object
- Object
- Elastic::SiteSearch::ResultSet
- 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
-
#errors ⇒ Object
readonly
a hash of errors for the search (for example filtering on a missing attribute) keyed by DocumentType slug.
-
#info ⇒ Object
readonly
a hash of extra query info (for example, facets and number of results) keyed by DocumentType slug.
-
#records ⇒ Object
readonly
a hash of results for the search keyed by DocumentType slug.
Instance Method Summary collapse
-
#[](document_type) ⇒ Object
Get results for the provided DocumentType.
-
#current_page ⇒ Object
Return the page of results for this ResultSet.
-
#document_types ⇒ Object
Return a list of DocumentType slugs represented in the ResultSet.
-
#facets(document_type) ⇒ Object
Return the search facets for the provided DocumentType.
-
#initialize(results) ⇒ ResultSet
constructor
Create a Elastic::SiteSearch::ResultSet from deserialized JSON.
-
#num_pages(document_type = nil) ⇒ Object
Return the number of pages.
-
#per_page ⇒ Object
Return the number of results per page.
-
#query ⇒ Object
Return the query used for this search.
-
#total_result_count(document_type) ⇒ Object
Return the total number of results for the query.
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
#errors ⇒ Object (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 |
#info ⇒ Object (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 |
#records ⇒ Object (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
29 30 31 |
# File 'lib/elastic/site-search/result_set.rb', line 29 def [](document_type) records[document_type] end |
#current_page ⇒ Object
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_types ⇒ Object
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.
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.
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_page ⇒ Object
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 |
#query ⇒ Object
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 |