Class: Gcloud::Search::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/search/document.rb,
lib/gcloud/search/document/list.rb

Overview

Document

A document is an object that stores data that can be searched. Each document has a #doc_id that is unique within its index, a #rank, and a list of #fields that contain typed data. Its field values can be accessed through hash-like methods such as #[] and #each.

Examples:

require "gcloud"

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

document = index.document "product-sku-000001"
document.add "price", 24.95
index.save document
document.rank #=> 1443648166
document["price"] #=> 24.95

See Also:

Defined Under Namespace

Classes: List

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ FieldValues

Retrieve the field values associated to a field name.

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

Parameters:

  • name (String)

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

Returns:


117
118
119
# File 'lib/gcloud/search/document.rb', line 117

def [] name
  @fields[name]
end

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

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

Parameters:

  • name (String)

    The name of the field.

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


177
178
179
# File 'lib/gcloud/search/document.rb', line 177

def add name, value, type: nil, lang: nil
  @fields[name].add value, type: type, lang: lang
end

#delete(name, &block) ⇒ Object

Deletes a field and all values. (See Fields#delete)

Examples:

require "gcloud"

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

document = index.document "product-sku-000001"
document.delete "description"

Parameters:

  • name (String)

    The name of the field.


196
197
198
# File 'lib/gcloud/search/document.rb', line 196

def delete name, &block
  @fields.delete name, &block
end

#doc_idObject

The unique identifier for the document. Can be set explicitly when the document is saved. (See Index#document and #doc_id=.) If missing, it is automatically assigned to the document when saved.


59
60
61
# File 'lib/gcloud/search/document.rb', line 59

def doc_id
  @raw["docId"]
end

#doc_id=(new_doc_id) ⇒ Object

Sets the unique identifier for the document.

Must contain only visible, printable ASCII characters (ASCII codes 33 through 126 inclusive) and be no longer than 500 characters. It cannot begin with an exclamation point (!), and it cannot begin and end with double underscores (__).


70
71
72
# File 'lib/gcloud/search/document.rb', line 70

def doc_id= new_doc_id
  @raw["docId"] = new_doc_id
end

#each(&block) ⇒ Object

Calls block once for each field, passing the field name and values pair as parameters. If no block is given an enumerator is returned instead. (See Fields#each)

Examples:

require "gcloud"

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

document = index.document "product-sku-000001"
puts "The document #{document.doc_id} has the following fields:"
document.each do |name, values|
  puts "* #{name}:"
  values.each do |value|
    puts "  * #{value} (#{value.type})"
  end
end

221
222
223
# File 'lib/gcloud/search/document.rb', line 221

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

#fieldsObject

The fields in the document. Each field has a name (String) and a list of values (FieldValues). (See Fields)


126
127
128
# File 'lib/gcloud/search/document.rb', line 126

def fields
  @fields
end

#namesObject

Returns a new array populated with all the field names. (See Fields#names)

Examples:

require "gcloud"

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

document = index.document "product-sku-000001"
puts "The document #{document.doc_id} has the following fields:"
document.names.each do |name|
  puts "* #{name}:"
end

242
243
244
# File 'lib/gcloud/search/document.rb', line 242

def names
  @fields.names
end

#rankObject

A positive integer which determines the default ordering of documents returned from a search. The rank can be set explicitly when the document is saved. (See Index#document and #rank=.) If missing, it is automatically assigned to the document when saved.


79
80
81
# File 'lib/gcloud/search/document.rb', line 79

def rank
  @raw["rank"]
end

#rank=(new_rank) ⇒ Object

Sets the rank of the document.

The same rank should not be assigned to many documents, and should never be assigned to more than 10,000 documents. By default (when it is not specified or set to 0), it is set at the time the document is created to the number of seconds since January 1, 2011. The rank can be used in Index#search options expressions, order, and fields, where it is referenced as rank.


92
93
94
# File 'lib/gcloud/search/document.rb', line 92

def rank= new_rank
  @raw["rank"] = new_rank
end