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.


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

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

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

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.


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

def empty?
  @values.empty?
end