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
35 36 37 38 |
# File 'lib/riak/search/result_document.rb', line 35 def initialize(client, raw) @client = client @raw = raw end |
Instance Attribute Details
#client ⇒ Riak::Client (readonly)
24 25 26 |
# File 'lib/riak/search/result_document.rb', line 24 def client @client end |
#raw ⇒ Hash (readonly)
Returns the de-serialized hash returned from Solr.
27 28 29 |
# File 'lib/riak/search/result_document.rb', line 27 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.
122 123 124 |
# File 'lib/riak/search/result_document.rb', line 122 def [](field_name) raw[field_name.to_s] end |
#bucket ⇒ Riak::Bucket
Returns the bucket containing the result.
51 52 53 |
# File 'lib/riak/search/result_document.rb', line 51 def bucket @bucket ||= bucket_type.bucket raw['_yz_rb'] end |
#bucket_type ⇒ Riak::BucketType
Returns the bucket type containing the result.
46 47 48 |
# File 'lib/riak/search/result_document.rb', line 46 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.
87 88 89 |
# File 'lib/riak/search/result_document.rb', line 87 def counter return crdt if check_type_class Riak::Crdt::Counter end |
#crdt ⇒ Riak::Crdt::Base
Returns the materialized CRDT.
76 77 78 79 80 |
# File 'lib/riak/search/result_document.rb', line 76 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.
70 71 72 |
# File 'lib/riak/search/result_document.rb', line 70 def crdt? type_class != Riak::RObject end |
#hyper_log_log ⇒ Riak::Crdt::HyperLogLog
If the result document describes a set, return it.
114 115 116 |
# File 'lib/riak/search/result_document.rb', line 114 def hyper_log_log return crdt if check_type_class Riak::Crdt::HyperLogLog end |
#key ⇒ String
Returns the key of the result.
41 42 43 |
# File 'lib/riak/search/result_document.rb', line 41 def key @key ||= raw['_yz_rk'] end |
#map ⇒ Riak::Crdt::Map
If the result document describes a map, return it.
96 97 98 |
# File 'lib/riak/search/result_document.rb', line 96 def map return crdt if check_type_class Riak::Crdt::Map end |
#object ⇒ Object
Returns an appropriate object, be it CRDT or K-V.
139 140 141 142 |
# File 'lib/riak/search/result_document.rb', line 139 def object return crdt if crdt? robject end |
#robject ⇒ Riak::RObject
Loads the RObject referred to by the result document.
129 130 131 132 133 134 135 136 |
# File 'lib/riak/search/result_document.rb', line 129 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.
56 57 58 |
# File 'lib/riak/search/result_document.rb', line 56 def score @score ||= Float(raw['score']) end |
#set ⇒ Riak::Crdt::Set
If the result document describes a set, return it.
105 106 107 |
# File 'lib/riak/search/result_document.rb', line 105 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.
65 66 67 |
# File 'lib/riak/search/result_document.rb', line 65 def type_class bucket_type.data_type_class || Riak::RObject end |