Class: Gcloud::Search::FieldValues

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

Overview

FieldValues

The list of values for a field.

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.

Each field on a document can have multiple values. FieldValues is the object that manages the multiple values. Values can be the same or different types; however, it cannot have multiple datetime (DateTime) or number (Float) values. (See FieldValue)

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["description"].each do |value|
  puts "* #{value} (#{value.type}) [#{value.lang}]"
end

See Also:

Instance Method Summary collapse

Instance Method Details

#[](index) ⇒ FieldValue?

Returns the element at index, or returns a subarray starting at the start index and continuing for length elements, or returns a subarray specified by range of indices.

Negative indices count backward from the end of the array (-1 is the last element). For start and range cases the starting index is just before an element. Additionally, an empty array is returned when the starting index for an element range is at the end of the array.

Returns:

  • (FieldValue, nil)

    Returns nil if the index (or starting index) are out of range.


78
79
80
# File 'lib/gcloud/search/field_values.rb', line 78

def [] index
  @values[index]
end

#add(value, type: nil, lang: nil) ⇒ FieldValue

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["sku"].add "product-sku-000001", type: :atom
document["description"].add "The best T-shirt ever.",
                            type: :text, lang: "en"
document["description"].add "<p>The best T-shirt ever.</p>",
                            type: :html, lang: "en"
document["price"].add 24.95

Parameters:

  • value (String, Datetime, Float)

    The value to add to the field.

  • type (Symbol)

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

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

Returns:


130
131
132
133
134
135
136
# File 'lib/gcloud/search/field_values.rb', line 130

def add value, type: nil, lang: nil
  new_field = FieldValue.new value, type: type, lang: lang, name: @name
  if [:datetime, :number].include? new_field.type
    @values.delete_if { |v| v.type == new_field.type }
  end
  @values << new_field
end

#delete(value, &block) ⇒ FieldValue?

Deletes all values that are equal to value.

Examples:

require "gcloud"

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

document = index.document "product-sku-000001"
document["description"].count #=> 2
document["description"].delete "The best T-shirt ever."
document["description"].count #=> 1

Parameters:

  • value (String)

    The value to remove from the list of values.

Returns:

  • (FieldValue, nil)

    The last deleted FieldValue, or nil if no matching value is found.


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

def delete value, &block
  fv = @values.detect { |v| v == value }
  @values.delete fv, &block
end

#delete_at(index) ⇒ FieldValue

Deletes the value at the specified index, returning that FieldValue, or nil if the index is out of range.

Examples:

require "gcloud"

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

document = index.document "product-sku-000001"
document["description"].count #=> 2
document["description"].delete_at 0
document["description"].count #=> 1

Parameters:

  • index (String)

    The index of the value to be removed from the list of values.

Returns:

  • (FieldValue)

    The deleted FieldValue found at the specified index, or # nil if the index is out of range.


185
186
187
# File 'lib/gcloud/search/field_values.rb', line 185

def delete_at index
  @values.delete_at index
end

#each(&block) ⇒ Object

Calls the given block once for each field value, passing the field value as a parameter.

An Enumerator is returned if no block is given.

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["description"].each do |value|
  puts "* #{value} (#{value.type}) [#{value.lang}]"
end

208
209
210
# File 'lib/gcloud/search/field_values.rb', line 208

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

#empty?Boolean

Returns true if there are no values.

Returns:

  • (Boolean)

214
215
216
# File 'lib/gcloud/search/field_values.rb', line 214

def empty?
  @values.empty?
end