Class: Swiftype::ResultSet
- Inherits:
-
Object
- Object
- Swiftype::ResultSet
- Defined in:
- lib/swiftype/result_set.rb
Overview
The Swiftype::ResultSet class represents a search or suggest result returned by the Swiftype 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 Swiftype::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 Swiftype::ResultSet from deserialized JSON.
19 20 21 22 23 |
# File 'lib/swiftype/result_set.rb', line 19 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
7 8 9 |
# File 'lib/swiftype/result_set.rb', line 7 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.
16 17 18 |
# File 'lib/swiftype/result_set.rb', line 16 def info @info end |
#records ⇒ Object (readonly)
a hash of results for the search keyed by DocumentType slug. Use ‘[]` to access results more easily.
11 12 13 |
# File 'lib/swiftype/result_set.rb', line 11 def records @records end |
Instance Method Details
#[](document_type) ⇒ Object
Get results for the provided DocumentType
28 29 30 |
# File 'lib/swiftype/result_set.rb', line 28 def [](document_type) records[document_type] end |
#current_page ⇒ Object
Return the page of results for this ResultSet
47 48 49 |
# File 'lib/swiftype/result_set.rb', line 47 def current_page info[info.keys.first]['current_page'] end |
#document_types ⇒ Object
Return a list of DocumentType slugs represented in the ResultSet.
33 34 35 |
# File 'lib/swiftype/result_set.rb', line 33 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.
42 43 44 |
# File 'lib/swiftype/result_set.rb', line 42 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.
64 65 66 67 68 69 70 |
# File 'lib/swiftype/result_set.rb', line 64 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.
52 53 54 |
# File 'lib/swiftype/result_set.rb', line 52 def per_page info[info.keys.first]['per_page'] end |
#query ⇒ Object
Return the query used for this search
78 79 80 |
# File 'lib/swiftype/result_set.rb', line 78 def query info[info.keys.first]['query'] end |
#total_result_count(document_type) ⇒ Object
Return the total number of results for the query
73 74 75 |
# File 'lib/swiftype/result_set.rb', line 73 def total_result_count(document_type) info[document_type]['total_result_count'] end |