class Riak::Search::ResultDocument

A single document from a Riak Search 2 response. Materializes the document fields into {Riak::BucketType}, {Riak::Bucket}, and {Riak::RObject} instances on demand.

Attributes

client[R]

@return [Riak::Client]

raw[R]

@return [Hash] the de-serialized hash returned from Solr

Public Class Methods

new(client, raw) click to toggle source

Iniitalize a {ResultDocument} with the client queried and the relevant part of the JSON returned from the search API.

This is automatically called by {Riak::Search::ResultCollection}#docs

@api private

# File lib/riak/search/result_document.rb, line 21
def initialize(client, raw)
  @client = client
  @raw = raw
end

Public Instance Methods

[](field_name) click to toggle source

Provides access to other parts of the result document without materializing them. Useful when querying non-default fields.

@return [String,Numeric] other search result document field

# File lib/riak/search/result_document.rb, line 108
def [](field_name)
  raw[field_name.to_s]
end
bucket() click to toggle source

@return [Riak::Bucket] the bucket containing the result

# File lib/riak/search/result_document.rb, line 37
def bucket
  @bucket ||= bucket_type.bucket raw['_yz_rb']
end
bucket_type() click to toggle source

@return [Riak::BucketType] the bucket type containing the result

# File lib/riak/search/result_document.rb, line 32
def bucket_type
  @bucket_type ||= client.bucket_type raw['_yz_rt']
end
counter() click to toggle source

If the result document describes a counter, return it.

@return [Riak::Crdt::Counter] @raise [Riak::CrdtError::NotACrdt] if the result is not a CRDT @raise [Riak::CrdtError::UnexpectedDataType] if the CRDT is not a counter

# File lib/riak/search/result_document.rb, line 73
def counter
  return crdt if check_type_class Riak::Crdt::Counter
end
crdt() click to toggle source

@raise [Riak::CrdtError::NotACrdt] if the result is not a CRDT @return [Riak::Crdt::Base] the materialized CRDT

# File lib/riak/search/result_document.rb, line 62
def crdt
  fail Riak::CrdtError::NotACrdt unless crdt?

  type_class.new bucket, key, bucket_type
end
crdt?() click to toggle source

@return [Boolean] if the object is a CRDT

# File lib/riak/search/result_document.rb, line 56
def crdt?
  type_class != Riak::RObject
end
hyper_log_log() click to toggle source

If the result document describes a set, return it.

@return [Riak::Crdt::HyperLogLog] @raise [Riak::CrdtError::NotACrdt] if the result is not a CRDT @raise [Riak::CrdtError::UnexpectedDataType] if the CRDT is not a hyper_log_log

# File lib/riak/search/result_document.rb, line 100
def hyper_log_log
  return crdt if check_type_class Riak::Crdt::HyperLogLog
end
key() click to toggle source

@return [String] the key of the result

# File lib/riak/search/result_document.rb, line 27
def key
  @key ||= raw['_yz_rk']
end
map() click to toggle source

If the result document describes a map, return it.

@return [Riak::Crdt::Map] @raise [Riak::CrdtError::NotACrdt] if the result is not a CRDT @raise [Riak::CrdtError::UnexpectedDataType] if the CRDT is not a map

# File lib/riak/search/result_document.rb, line 82
def map
  return crdt if check_type_class Riak::Crdt::Map
end
object() click to toggle source

Returns an appropriate object, be it CRDT or K-V.

# File lib/riak/search/result_document.rb, line 125
def object
  return crdt if crdt?
  robject
end
robject() click to toggle source

Loads the {Riak::RObject} referred to by the result document.

@return [Riak::RObject]

# File lib/riak/search/result_document.rb, line 115
def robject
  if crdt?
    fail Riak::SearchError::UnexpectedResultError.
          new(Riak::RObject, type_class)
  end

  @robject ||= bucket.get key
end
score() click to toggle source

@return [Numeric] the score of the match

# File lib/riak/search/result_document.rb, line 42
def score
  @score ||= Float(raw['score'])
end
set() click to toggle source

If the result document describes a set, return it.

@return [Riak::Crdt::Set] @raise [Riak::CrdtError::NotACrdt] if the result is not a CRDT @raise [Riak::CrdtError::UnexpectedDataType] if the CRDT is not a set

# File lib/riak/search/result_document.rb, line 91
def set
  return crdt if check_type_class Riak::Crdt::Set
end
type_class() click to toggle source

Determining if the object is a CRDT or regular K-V object requires figuring out what data type the bucket type contains. If the bucket type has no data type, treat it as a regular K-V object.

@return [Class] the class of the object referred to by the search result

# File lib/riak/search/result_document.rb, line 51
def type_class
  bucket_type.data_type_class || Riak::RObject
end

Private Instance Methods

check_type_class(klass) click to toggle source
# File lib/riak/search/result_document.rb, line 132
def check_type_class(klass)
  return true if type_class == klass
  fail Riak::CrdtError::NotACrdt if type_class == Riak::RObject
  fail Riak::CrdtError::UnexpectedDataType.new(klass, type_class)
end