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.

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new FieldValue object.

Parameters

value

The value to add to the field. (String or Datetime or Float)

type

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. (Symbol)

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.

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

name

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



87
88
89
90
91
92
93
94
95
96
# File 'lib/gcloud/search/field_value.rb', line 87

def initialize value, type: nil, lang: nil, name: nil #:nodoc:
  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.



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

def lang
  @lang
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.from_raw(field_value, name = nil) ⇒ Object

Create a new FieldValue instance from a value Hash.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gcloud/search/field_value.rb', line 115

def self.from_raw field_value, name = nil #:nodoc:
  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)


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

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

#to_rawObject

Create a raw Hash object containing the field value.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/gcloud/search/field_value.rb', line 134

def to_raw #:nodoc:
  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

Get the original value object.



109
110
111
# File 'lib/gcloud/search/field_value.rb', line 109

def value #:nodoc:
  __getobj__
end