Class: Gcloud::Search::Result

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeResult

Returns a new instance of Result.



28
29
30
31
# File 'lib/gcloud/search/result.rb', line 28

def initialize
  @fields = Fields.new
  @raw = {}
end

Class Method Details

.from_hash(hash) ⇒ Object



149
150
151
152
153
154
# File 'lib/gcloud/search/result.rb', line 149

def self.from_hash hash
  result = new
  result.instance_variable_set "@raw", hash
  result.instance_variable_set "@fields", Fields.from_raw(hash["fields"])
  result
end

Instance Method Details

#[](name) ⇒ FieldValue

Retrieve the field values associated to a field name.

Examples:

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

Parameters:

  • name (String)

    The name of the field. New values will be configured with this name.

Returns:



71
72
73
# File 'lib/gcloud/search/result.rb', line 71

def [] name
  @fields[name]
end

#doc_idString

The unique identifier of the document referenced in the search result.

Returns:

  • (String)


37
38
39
# File 'lib/gcloud/search/result.rb', line 37

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)

Examples:

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


106
107
108
# File 'lib/gcloud/search/result.rb', line 106

def each &block
  @fields.each(&block)
end

#fieldsObject

The fields in the search result. Each field has a name (String) and a list of values (FieldValues). (See Fields)



80
81
82
# File 'lib/gcloud/search/result.rb', line 80

def fields
  @fields
end

#inspectObject



136
137
138
139
140
141
142
143
144
145
# File 'lib/gcloud/search/result.rb', line 136

def inspect
  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

#namesArray<String>

Returns a new array populated with all the field names. (See Fields#names)

Examples:

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

Returns:

  • (Array<String>)


130
131
132
# File 'lib/gcloud/search/result.rb', line 130

def names
  @fields.names
end

#to_hashObject



158
159
160
161
162
# File 'lib/gcloud/search/result.rb', line 158

def to_hash
  hash = @raw.dup
  hash["fields"] = @fields.to_raw
  hash
end

#tokenString

The token for the next page of results.

Returns:

  • (String)


45
46
47
# File 'lib/gcloud/search/result.rb', line 45

def token
  @raw["nextPageToken"]
end