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)

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

For more information see Documents and fields.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, values = []) ⇒ FieldValues

Create a new FieldValues object.

Parameters

name

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

values

A list of values to add to the field. (Array of FieldValue objects)



65
66
67
68
# File 'lib/gcloud/search/field_values.rb', line 65

def initialize name, values = [] # :nodoc:
  @name = name
  @values = values
end

Class Method Details

.from_raw(name, values) ⇒ Object

Create a new FieldValues instance from a name and values Hash.



251
252
253
254
# File 'lib/gcloud/search/field_values.rb', line 251

def self.from_raw name, values #:nodoc:
  field_values = values.map { |value| FieldValue.from_raw value, name }
  FieldValues.new name, field_values
end

Instance Method Details

#[](index) ⇒ Object

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 nil if the index (or starting index) are out of range.



81
82
83
# File 'lib/gcloud/search/field_values.rb', line 81

def [] index
  @values[index]
end

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

Add a new value. The field name will be added to the value object. 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

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)

Returns

FieldValue

Example

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


146
147
148
149
150
151
152
# File 'lib/gcloud/search/field_values.rb', line 146

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) ⇒ Object

Deletes all values that are equal to value.

Parameters

value

The value to remove from the list of values.

Returns

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

Example

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


181
182
183
184
# File 'lib/gcloud/search/field_values.rb', line 181

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

#delete_at(index) ⇒ Object

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

Deletes all values that are equal to value.

Parameters

index

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

Returns

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

Example

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


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

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.

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


239
240
241
# File 'lib/gcloud/search/field_values.rb', line 239

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

#empty?Boolean

Returns true if there are no values.

Returns:

  • (Boolean)


245
246
247
# File 'lib/gcloud/search/field_values.rb', line 245

def empty?
  @values.empty?
end

#to_rawObject

Create a raw Hash object containing all the field values.



258
259
260
# File 'lib/gcloud/search/field_values.rb', line 258

def to_raw #:nodoc:
  { "values" => @values.map(&:to_raw) }
end