Class: Gcloud::Search::Result::List
- Inherits:
-
Array
- Object
- Array
- Gcloud::Search::Result::List
- Defined in:
- lib/gcloud/search/result/list.rb
Overview
Result::List is a special case Array with additional values.
Instance Attribute Summary collapse
-
#matched_count ⇒ Object
readonly
The number of documents that match the query.
-
#token ⇒ Object
readonly
If not empty, indicates that there are more records that match the request and this value should be passed to continue.
Class Method Summary collapse
-
.from_response(resp, index, query, search_options) ⇒ Object
New Result::List from a response object.
Instance Method Summary collapse
-
#all ⇒ Object
Retrieves all results by repeatedly loading pages until #next? returns false.
-
#initialize(arr = []) ⇒ List
constructor
Create a new Result::List with an array of Result instances.
-
#next ⇒ Object
Retrieve the next page of results.
-
#next? ⇒ Boolean
Whether there a next page of results.
Constructor Details
#initialize(arr = []) ⇒ List
Create a new Result::List with an array of Result instances.
36 37 38 |
# File 'lib/gcloud/search/result/list.rb', line 36 def initialize arr = [] super arr end |
Instance Attribute Details
#matched_count ⇒ Object (readonly)
The number of documents that match the query. It is greater than or equal to the number of documents actually returned. This is an approximation and not an exact count unless it is less than or equal to the Index#search matched_count_accuracy option.
32 33 34 |
# File 'lib/gcloud/search/result/list.rb', line 32 def matched_count @matched_count end |
#token ⇒ Object (readonly)
If not empty, indicates that there are more records that match the request and this value should be passed to continue.
25 26 27 |
# File 'lib/gcloud/search/result/list.rb', line 25 def token @token end |
Class Method Details
.from_response(resp, index, query, search_options) ⇒ Object
New Result::List from a response object.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gcloud/search/result/list.rb', line 68 def self.from_response resp, index, query, #:nodoc: data = JSON.parse resp.body results = new(Array(data["results"]).map do |raw| Result.from_hash raw end) results.instance_eval do @token = data["results"].last["nextPageToken"] @matched_count = data["matchedCount"] @index = index @query = query @search_options = end results rescue JSON::ParserError ApiError.from_response_status resp end |
Instance Method Details
#all ⇒ Object
Retrieves all results by repeatedly loading pages until #next? returns false. Returns the list instance for method chaining.
57 58 59 60 61 62 63 64 |
# File 'lib/gcloud/search/result/list.rb', line 57 def all while next? next_results = self.next push(*next_results) self.token = next_results.token end self end |
#next ⇒ Object
Retrieve the next page of results.
48 49 50 51 52 |
# File 'lib/gcloud/search/result/list.rb', line 48 def next return nil unless next? ensure_index! @index.search @query, @search_options.merge(token: token) end |
#next? ⇒ Boolean
Whether there a next page of results.
42 43 44 |
# File 'lib/gcloud/search/result/list.rb', line 42 def next? !token.nil? end |