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:

Instance Method Summary collapse

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

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

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

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"

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

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

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

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

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

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