Class: Riak::Search::ResultDocument

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#clientRiak::Client (readonly)

Returns:



10
11
12
# File 'lib/riak/search/result_document.rb', line 10

def client
  @client
end

#rawHash (readonly)

Returns the de-serialized hash returned from Solr.

Returns:

  • (Hash)

    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.

Returns:

  • (String, Numeric)

    other search result document field



108
109
110
# File 'lib/riak/search/result_document.rb', line 108

def [](field_name)
  raw[field_name.to_s]
end

#bucketRiak::Bucket

Returns the bucket containing the result.

Returns:



37
38
39
# File 'lib/riak/search/result_document.rb', line 37

def bucket
  @bucket ||= bucket_type.bucket raw['_yz_rb']
end

#bucket_typeRiak::BucketType

Returns the bucket type containing the result.

Returns:



32
33
34
# File 'lib/riak/search/result_document.rb', line 32

def bucket_type
  @bucket_type ||= client.bucket_type raw['_yz_rt']
end

#counterRiak::Crdt::Counter

If the result document describes a counter, return it.

Returns:

Raises:



73
74
75
# File 'lib/riak/search/result_document.rb', line 73

def counter
  return crdt if check_type_class Riak::Crdt::Counter
end

#crdtRiak::Crdt::Base

Returns the materialized CRDT.

Returns:

Raises:



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.

Returns:

  • (Boolean)

    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_logRiak::Crdt::HyperLogLog

If the result document describes a set, return it.

Returns:

Raises:



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

#keyString

Returns the key of the result.

Returns:

  • (String)

    the key of the result



27
28
29
# File 'lib/riak/search/result_document.rb', line 27

def key
  @key ||= raw['_yz_rk']
end

#mapRiak::Crdt::Map

If the result document describes a map, return it.

Returns:

Raises:



82
83
84
# File 'lib/riak/search/result_document.rb', line 82

def map
  return crdt if check_type_class Riak::Crdt::Map
end

#objectObject

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

#robjectRiak::RObject

Loads the RObject referred to by the result document.

Returns:



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

#scoreNumeric

Returns the score of the match.

Returns:

  • (Numeric)

    the score of the match



42
43
44
# File 'lib/riak/search/result_document.rb', line 42

def score
  @score ||= Float(raw['score'])
end

#setRiak::Crdt::Set

If the result document describes a set, return it.

Returns:

Raises:



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_classClass

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.

Returns:

  • (Class)

    the class of the object referred to by the search result



51
52
53
# File 'lib/riak/search/result_document.rb', line 51

def type_class
  bucket_type.data_type_class || Riak::RObject
end