Class: Riak::Search::ResultDocument
- Defined in:
- lib/riak/search/result_document.rb
Overview
A single document from a Riak Search 2 response. Materializes the document fields into BucketType, Bucket, and RObject instances on demand.
Instance Attribute Summary collapse
- #client ⇒ Riak::Client readonly
-
#raw ⇒ Hash
readonly
The de-serialized hash returned from Solr.
Instance Method Summary collapse
-
#[](field_name) ⇒ String, Numeric
Provides access to other parts of the result document without materializing them.
-
#bucket ⇒ Riak::Bucket
The bucket containing the result.
-
#bucket_type ⇒ Riak::BucketType
The bucket type containing the result.
-
#counter ⇒ Riak::Crdt::Counter
If the result document describes a counter, return it.
-
#crdt ⇒ Riak::Crdt::Base
The materialized CRDT.
-
#crdt? ⇒ Boolean
If the object is a CRDT.
-
#hyper_log_log ⇒ Riak::Crdt::HyperLogLog
If the result document describes a set, return it.
-
#initialize(client, raw) ⇒ ResultDocument
constructor
private
Iniitalize a ResultDocument with the client queried and the relevant part of the JSON returned from the search API.
-
#key ⇒ String
The key of the result.
-
#map ⇒ Riak::Crdt::Map
If the result document describes a map, return it.
-
#object ⇒ Object
Returns an appropriate object, be it CRDT or K-V.
-
#robject ⇒ Riak::RObject
Loads the RObject referred to by the result document.
-
#score ⇒ Numeric
The score of the match.
-
#set ⇒ Riak::Crdt::Set
If the result document describes a set, return it.
-
#type_class ⇒ Class
Determining if the object is a CRDT or regular K-V object requires figuring out what data type the bucket type contains.
Constructor Details
#initialize(client, raw) ⇒ ResultDocument
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Iniitalize a Riak::Search::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
21 22 23 24 |
# File 'lib/riak/search/result_document.rb', line 21 def initialize(client, raw) @client = client @raw = raw end |
Instance Attribute Details
#client ⇒ Riak::Client (readonly)
10 11 12 |
# File 'lib/riak/search/result_document.rb', line 10 def client @client end |
#raw ⇒ Hash (readonly)
Returns the de-serialized hash returned from Solr.
13 14 15 |
# File 'lib/riak/search/result_document.rb', line 13 def raw @raw end |
Instance Method Details
#[](field_name) ⇒ String, Numeric
Provides access to other parts of the result document without materializing them. Useful when querying non-default fields.
108 109 110 |
# File 'lib/riak/search/result_document.rb', line 108 def [](field_name) raw[field_name.to_s] end |
#bucket ⇒ Riak::Bucket
Returns the bucket containing the result.
37 38 39 |
# File 'lib/riak/search/result_document.rb', line 37 def bucket @bucket ||= bucket_type.bucket raw['_yz_rb'] end |
#bucket_type ⇒ Riak::BucketType
Returns the bucket type containing the result.
32 33 34 |
# File 'lib/riak/search/result_document.rb', line 32 def bucket_type @bucket_type ||= client.bucket_type raw['_yz_rt'] end |
#counter ⇒ Riak::Crdt::Counter
If the result document describes a counter, return it.
73 74 75 |
# File 'lib/riak/search/result_document.rb', line 73 def counter return crdt if check_type_class Riak::Crdt::Counter end |
#crdt ⇒ Riak::Crdt::Base
Returns the materialized CRDT.
62 63 64 65 66 |
# 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? ⇒ Boolean
Returns if the object is a CRDT.
56 57 58 |
# File 'lib/riak/search/result_document.rb', line 56 def crdt? type_class != Riak::RObject end |
#hyper_log_log ⇒ Riak::Crdt::HyperLogLog
If the result document describes a set, return it.
100 101 102 |
# File 'lib/riak/search/result_document.rb', line 100 def hyper_log_log return crdt if check_type_class Riak::Crdt::HyperLogLog end |
#key ⇒ String
Returns the key of the result.
27 28 29 |
# File 'lib/riak/search/result_document.rb', line 27 def key @key ||= raw['_yz_rk'] end |
#map ⇒ Riak::Crdt::Map
If the result document describes a map, return it.
82 83 84 |
# File 'lib/riak/search/result_document.rb', line 82 def map return crdt if check_type_class Riak::Crdt::Map end |
#object ⇒ Object
Returns an appropriate object, be it CRDT or K-V.
125 126 127 128 |
# File 'lib/riak/search/result_document.rb', line 125 def object return crdt if crdt? robject end |
#robject ⇒ Riak::RObject
Loads the RObject referred to by the result document.
115 116 117 118 119 120 121 122 |
# 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 ⇒ Numeric
Returns the score of the match.
42 43 44 |
# File 'lib/riak/search/result_document.rb', line 42 def score @score ||= Float(raw['score']) end |
#set ⇒ Riak::Crdt::Set
If the result document describes a set, return it.
91 92 93 |
# File 'lib/riak/search/result_document.rb', line 91 def set return crdt if check_type_class Riak::Crdt::Set end |
#type_class ⇒ Class
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.
51 52 53 |
# File 'lib/riak/search/result_document.rb', line 51 def type_class bucket_type.data_type_class || Riak::RObject end |