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



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

#clientRiak::Client (readonly)

Returns:



24
25
26
# File 'lib/riak/search/result_document.rb', line 24

def client
  @client
end

#rawHash (readonly)

Returns the de-serialized hash returned from Solr.

Returns:

  • (Hash)

    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.

Returns:

  • (String, Numeric)

    other search result document field



122
123
124
# File 'lib/riak/search/result_document.rb', line 122

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

#bucketRiak::Bucket

Returns the bucket containing the result.

Returns:



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

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

#bucket_typeRiak::BucketType

Returns the bucket type containing the result.

Returns:



46
47
48
# File 'lib/riak/search/result_document.rb', line 46

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:



87
88
89
# File 'lib/riak/search/result_document.rb', line 87

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

#crdtRiak::Crdt::Base

Returns the materialized CRDT.

Returns:

Raises:



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.

Returns:

  • (Boolean)

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

If the result document describes a set, return it.

Returns:

Raises:



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

#keyString

Returns the key of the result.

Returns:

  • (String)

    the key of the result



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

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

#mapRiak::Crdt::Map

If the result document describes a map, return it.

Returns:

Raises:



96
97
98
# File 'lib/riak/search/result_document.rb', line 96

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

#objectObject

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

#robjectRiak::RObject

Loads the RObject referred to by the result document.

Returns:



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

#scoreNumeric

Returns the score of the match.

Returns:

  • (Numeric)

    the score of the match



56
57
58
# File 'lib/riak/search/result_document.rb', line 56

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

#setRiak::Crdt::Set

If the result document describes a set, return it.

Returns:

Raises:



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_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



65
66
67
# File 'lib/riak/search/result_document.rb', line 65

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