Class: Gcloud::Search::FieldValue

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/search/field_value.rb

Overview

# FieldValue

FieldValue is used to represent a value that belongs to a field. (See Fields and FieldValues)

A field value must have a type. A value that is a Numeric will default to ‘:number`, while a DateTime will default to `:datetime`. If a type is not provided it will be determined by looking at the value.

String values (text, html, atom) can also specify a lang value, which is an [ISO 639-1 code](en.wikipedia.org/wiki/List_of_ISO_639-1_codes).

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 Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, type: nil, lang: nil, name: nil) ⇒ FieldValue

Returns a new instance of FieldValue.

Parameters:

  • value (String, Datetime, Float)

    The value to add to the field.

  • type (Symbol) (defaults to: nil)

    The type of the field value. A field can have multiple values with same or different types; however, it cannot have multiple Timestamp or number values.

    The following values are supported:

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

  • name (String) (defaults to: nil)

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



81
82
83
84
85
86
87
88
89
90
# File 'lib/gcloud/search/field_value.rb', line 81

def initialize value, type: nil, lang: nil, name: nil
  super value
  if type
    @type = type.to_s.downcase.to_sym
  else
    @type = infer_type
  end
  @lang = lang if string_type?
  @name = name
end

Instance Attribute Details

#langObject (readonly)

Returns the value of attribute lang.



49
50
51
# File 'lib/gcloud/search/field_value.rb', line 49

def lang
  @lang
end

#nameObject (readonly)

Returns the value of attribute name.



49
50
51
# File 'lib/gcloud/search/field_value.rb', line 49

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



49
50
51
# File 'lib/gcloud/search/field_value.rb', line 49

def type
  @type
end

Class Method Details

.from_raw(field_value, name = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gcloud/search/field_value.rb', line 109

def self.from_raw field_value, name = nil
  value = field_value["stringValue"]
  type = field_value["stringFormat"]
  if field_value["timestampValue"]
    value = DateTime.parse(field_value["timestampValue"])
    type = :datetime
  elsif field_value["geoValue"]
    value = field_value["geoValue"]
    type = :geo
  elsif field_value["numberValue"]
    value = Float(field_value["numberValue"])
    type = :number
  end
  fail "No value found in #{raw_field.inspect}" if value.nil?
  new value, type: type, lang: field_value["lang"], name: name
end

Instance Method Details

#string_type?Boolean

Determines if the value a string type. The value is text or html or atom (or default).

Returns:

  • (Boolean)


97
98
99
# File 'lib/gcloud/search/field_value.rb', line 97

def string_type?
  [:atom, :default, :html, :text].include? type
end

#to_rawObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gcloud/search/field_value.rb', line 128

def to_raw
  case type
  when :atom, :default, :html, :text
    {
      "stringFormat" => type.to_s.upcase,
      "lang" => lang,
      "stringValue" => to_s
    }.delete_if { |_, v| v.nil? }
  when :geo
    { "geoValue" => to_s }
  when :number
    { "numberValue" => to_f }
  when :datetime
    { "timestampValue" => rfc3339 }
  end
end

#valueObject



103
104
105
# File 'lib/gcloud/search/field_value.rb', line 103

def value
  __getobj__
end