Class: Gcloud::Search::Result
- Inherits:
-
Object
- Object
- Gcloud::Search::Result
- Defined in:
- lib/gcloud/search/result.rb,
lib/gcloud/search/result/list.rb
Overview
Result
See Gcloud#search
Defined Under Namespace
Classes: List
Class Method Summary collapse
-
.from_hash(hash) ⇒ Object
New Result from a raw data object.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Retrieve the field values associated to a field name.
-
#doc_id ⇒ Object
The unique identifier of the document referenced in the search result.
-
#each(&block) ⇒ Object
Calls block once for each field, passing the field name and values pair as parameters.
-
#fields ⇒ Object
The fields in the search result.
-
#initialize ⇒ Result
constructor
Creates a new Result instance.
-
#inspect ⇒ Object
Override to keep working in interactive shells manageable.
-
#names ⇒ Object
Returns a new array populated with all the field names.
-
#to_hash ⇒ Object
Returns the Result data as a hash.
-
#token ⇒ Object
The token for the next page of results.
Constructor Details
#initialize ⇒ Result
Creates a new Result instance.
28 29 30 31 |
# File 'lib/gcloud/search/result.rb', line 28 def initialize #:nodoc: @fields = Fields.new @raw = {} end |
Class Method Details
.from_hash(hash) ⇒ Object
New Result from a raw data object.
153 154 155 156 157 158 |
# File 'lib/gcloud/search/result.rb', line 153 def self.from_hash hash #:nodoc: result = new result.instance_variable_set "@raw", hash result.instance_variable_set "@fields", Fields.from_raw(hash["fields"]) result end |
Instance Method Details
#[](name) ⇒ Object
Retrieve the field values associated to a field name.
Parameters
name-
The name of the field. New values will be configured with this name. (
String)
Returns
FieldValue
Example
require "gcloud"
gcloud = Gcloud.new
search = gcloud.search
index = search.index "products"
documents = index.search "best T-shirt ever"
document = documents.first
puts "The best match for your search is:"
document["description"].each do |value|
puts "* #{value} (#{value.type}) [#{value.lang}]"
end
73 74 75 |
# File 'lib/gcloud/search/result.rb', line 73 def [] name @fields[name] end |
#doc_id ⇒ Object
The unique identifier of the document referenced in the search result.
35 36 37 |
# File 'lib/gcloud/search/result.rb', line 35 def doc_id @raw["docId"] end |
#each(&block) ⇒ Object
Calls block once for each field, passing the field name and values pair as parameters. If no block is given an enumerator is returned instead. (See Fields#each)
Example
require "gcloud"
gcloud = Gcloud.new
search = gcloud.search
index = search.index "products"
documents = index.search "best T-shirt ever"
document = documents.first
puts "The best match for your search is:"
document.each do |name, values|
puts "* #{name}:"
values.each do |value|
puts " * #{value} (#{value.type})"
end
end
113 114 115 |
# File 'lib/gcloud/search/result.rb', line 113 def each &block @fields.each(&block) end |
#fields ⇒ Object
The fields in the search result. Each field has a name (String) and a list of values (FieldValues). (See Fields)
84 85 86 |
# File 'lib/gcloud/search/result.rb', line 84 def fields @fields end |
#inspect ⇒ Object
Override to keep working in interactive shells manageable.
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/gcloud/search/result.rb', line 140 def inspect #:nodoc: insp_token = "" if token trunc_token = "#{token[0, 8]}...#{token[-5..-1]}" trunc_token = token if token.length < 20 insp_token = ", token: #{trunc_token.inspect}" end insp_fields = ", fields: (#{fields.names.map(&:inspect).join ', '})" "#{self.class}(doc_id: #{doc_id.inspect}#{insp_token}#{insp_fields})" end |
#names ⇒ Object
Returns a new array populated with all the field names. (See Fields#names)
require "gcloud"
gcloud = Gcloud.new
search = gcloud.search
index = search.index "products"
documents = index.search "best T-shirt ever"
document = documents.first
puts "The best match has the following fields:"
document.names.each do |name|
puts "* #{name}:"
end
134 135 136 |
# File 'lib/gcloud/search/result.rb', line 134 def names @fields.names end |
#to_hash ⇒ Object
Returns the Result data as a hash
162 163 164 165 166 |
# File 'lib/gcloud/search/result.rb', line 162 def to_hash #:nodoc: hash = @raw.dup hash["fields"] = @fields.to_raw hash end |
#token ⇒ Object
The token for the next page of results.
41 42 43 |
# File 'lib/gcloud/search/result.rb', line 41 def token @raw["nextPageToken"] end |