Class: Gcloud::Search::Fields

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gcloud/search/fields.rb

Overview

Fields

Fields is the object that provides access to a document’s fields.

Each field has a name (String) and a list of values. Each field name consists of only ASCII characters, must be unique within the document and is case sensitive. A field name must start with a letter and can contain letters, digits, or underscore, with a maximum of 500 characters.

A field can have multiple values with same or different types; however, it cannot have multiple datetime (DateTime) or number (Float) values. (See FieldValues and FieldValue)

require "gcloud"

gcloud = Gcloud.new
search = gcloud.search
index = search.index "products"

document = index.document "product-sku-000001"
puts "The document #{document.doc_id} has the following fields:"
document.names.each do |name|
  puts "* #{name}:"
  document[name].each do |value|
    puts "  * #{value} (#{value.type})"
  end
end

For more information see Documents and fields.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFields

Create a new empty fields object.



58
59
60
# File 'lib/gcloud/search/fields.rb', line 58

def initialize #:nodoc:
  @hash = {}
end

Class Method Details

.from_raw(raw) ⇒ Object

Create a new Fields instance from a raw Hash.



230
231
232
233
234
235
236
237
238
# File 'lib/gcloud/search/fields.rb', line 230

def self.from_raw raw #:nodoc:
  hsh = {}
  raw.each do |k, v|
    hsh[k] = FieldValues.from_raw k, v["values"]
  end unless raw.nil?
  fields = new
  fields.instance_variable_set "@hash", hsh
  fields
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

FieldValues

Example

require "gcloud"

gcloud = Gcloud.new
search = gcloud.search
index = search.index "products"

document = index.document "product-sku-000001"
puts "The document description is:"
document.fields["description"].each do |value|
  puts "* #{value} (#{value.type}) [#{value.lang}]"
end


89
90
91
# File 'lib/gcloud/search/fields.rb', line 89

def [] name
  @hash[name] ||= FieldValues.new name
end

#add(name, value, type: nil, lang: nil) ⇒ Object

Add a new value. If the field name does not exist it will be added. If the field value is a DateTime or Numeric, or the type is set to :datetime or :number, then the added value will replace any existing values of the same type (since there can be only one).

Parameters

name

The name of the field. (String)

value

The value to add to the field. (String or Datetime or Float)

type

The type of the field value. An attempt is made to set the correct type when this option is missing, although it must be provided for :geo values. A field can have multiple values with same or different types; however, it cannot have multiple :datetime or :number values. (Symbol)

The following values are supported:

  • :default - The value is a string. The format will be automatically detected. This is the default value for strings.

  • :text - The value is a string with maximum length 1024**2 characters.

  • :html - The value is an HTML-formatted string with maximum length 1024**2 characters.

  • :atom - The value is a string with maximum length 500 characters.

  • :geo - The value is a point on earth described by latitude and longitude coordinates, represented in string with any of the listed ways of writing coordinates.

  • :datetime - The value is a DateTime.

  • :number - The value is a Numeric between -2,147,483,647 and 2,147,483,647. The value will be stored as a double precision floating point value in Cloud Search.

lang

The language of a string value. Must be a valid ISO 639-1 code. (String)

Example

require "gcloud"

gcloud = Gcloud.new
search = gcloud.search
index = search.index "products"

document = index.document "product-sku-000001"
document.fields.add "sku", "product-sku-000001", type: :atom
document.fields.add "description", "The best T-shirt ever.",
                    type: :text, lang: "en"
document.fields.add "description", "<p>The best T-shirt ever.</p>",
                    type: :html, lang: "en"
document.fields.add "price", 24.95


152
153
154
155
# File 'lib/gcloud/search/fields.rb', line 152

def add name, value, type: nil, lang: nil
  @hash[name] ||= FieldValues.new name
  @hash[name].add value, type: type, lang: lang
end

#delete(name, &block) ⇒ Object

Deletes a field and all values.

Parameters

name

The name of the field. (String)

Example

require "gcloud"

gcloud = Gcloud.new
search = gcloud.search
index = search.index "products"

document = index.document "product-sku-000001"
document.fields.delete "description"


178
179
180
# File 'lib/gcloud/search/fields.rb', line 178

def delete name, &block
  @hash.delete name, &block
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.

Example

require "gcloud"

gcloud = Gcloud.new
search = gcloud.search
index = search.index "products"

document = index.document "product-sku-000001"
puts "The document #{document.doc_id} has the following fields:"
document.fields.each do |name, values|
  puts "* #{name}:"
  values.each do |value|
    puts "  * #{value} (#{value.type})"
  end
end


203
204
205
206
# File 'lib/gcloud/search/fields.rb', line 203

def each &block
  # Only yield fields that have values.
  fields_with_values.each(&block)
end

#namesObject

Returns a new array populated with all the field names.

require "gcloud"

gcloud = Gcloud.new
search = gcloud.search
index = search.index "products"

document = index.document "product-sku-000001"
puts "The document #{document.doc_id} has the following fields:"
document.fields.names.each do |name|
  puts "* #{name}:"
end


223
224
225
226
# File 'lib/gcloud/search/fields.rb', line 223

def names
  # Only return fields that have values.
  fields_with_values.keys
end

#to_rawObject

Create a raw Hash object containing all the field names and values.



242
243
244
245
246
247
248
# File 'lib/gcloud/search/fields.rb', line 242

def to_raw #:nodoc:
  hsh = {}
  @hash.each do |k, v|
    hsh[k] = v.to_raw unless v.empty?
  end
  hsh
end