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)

Examples:

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

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFields

Returns a new instance of Fields.



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

def initialize
  @hash = {}
end

Class Method Details

.from_raw(raw) ⇒ Object



208
209
210
211
212
213
214
215
216
# File 'lib/gcloud/search/fields.rb', line 208

def self.from_raw raw
  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) ⇒ FieldValues

Retrieve the field values associated to a field name.

Examples:

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

Parameters:

  • name (String)

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

Returns:



84
85
86
# File 'lib/gcloud/search/fields.rb', line 84

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).

Examples:

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

Parameters:

  • name (String)

    The name of the field.

  • value (String, Datetime, Float)

    The value to add to the field.

  • type (Symbol) (defaults to: nil)

    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.

    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](en.wikipedia.org/wiki/Geographic_coordinate_conversion).

    • ‘: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 (String) (defaults to: nil)

    The language of a string value. Must be a valid [ISO 639-1 code](en.wikipedia.org/wiki/List_of_ISO_639-1_codes).



136
137
138
139
# File 'lib/gcloud/search/fields.rb', line 136

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.

Examples:

require "gcloud"

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

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

Parameters:

  • name (String)

    The name of the field.



156
157
158
# File 'lib/gcloud/search/fields.rb', line 156

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.

Examples:

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


180
181
182
183
# File 'lib/gcloud/search/fields.rb', line 180

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.

Examples:

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


201
202
203
204
# File 'lib/gcloud/search/fields.rb', line 201

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

#to_rawObject

values.



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

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