Class: Riak::Search::ResultCollection
- Defined in:
- lib/riak/search/result_collection.rb
Overview
A collection of Riak Search 2 (“Yokozuna”) results. Provides direct access to the RObject instances found, and access through the #docs method to the results as returned by Solr.
Instance Attribute Summary collapse
- #client ⇒ Riak::Client readonly
-
#length ⇒ Integer
readonly
The number of documents in this collection.
-
#max_score ⇒ Numeric
readonly
The maximum score found by Solr.
-
#num_found ⇒ Integer
readonly
The total number of documents matched, including ones not returned due to row-limiting.
-
#raw ⇒ Hash
readonly
The de-serialzed hash returned from Solr.
Instance Method Summary collapse
-
#[](index) ⇒ Riak::RObject, NilClass
The found object, or nil if the index is out of range.
-
#counters ⇒ Array<Riak::Crdt::Counter] counter objects
Materializes [Riak::Crdt::Counter] results.
-
#crdts ⇒ Array<Riak::Crdt::Base>
Materializes [Riak::Crdt::Base] subclasses from any CRDT results.
-
#docs ⇒ Array<Riak::Search::ResultDocument>
Access the individual documents from the search results.
-
#each_robject {|robject| ... } ⇒ Enumerator<Riak::RObject>
Enumerable-compatible iterator method.
-
#empty? ⇒ Boolean
Does this collection contain any documents?.
-
#first ⇒ Riak::RObject, NilClass
The first found object, or nil if the index is out of range.
-
#initialize(client, raw_results) ⇒ ResultCollection
constructor
private
Initialize a ResultCollection with the client queried and the raw JSON returned from the search API.
-
#maps ⇒ Array<Riak::Crdt::Map] map objects
Materializes [Riak::Crdt::Map] results.
-
#objects ⇒ Array
Materializes and returns an array of objects from search results.
-
#robjects ⇒ Array<Riak::RObject>
Materializes [Riak::RObject]s from any key-value results.
-
#sets ⇒ Array<Riak::Crdt::Set>
Materializes [Riak::Crdt::Set] results.
Constructor Details
#initialize(client, raw_results) ⇒ ResultCollection
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.
Initialize a Riak::Search::ResultCollection with the client queried and the raw JSON returned from the search API.
This is automatically called by Query#results
31 32 33 34 35 36 37 |
# File 'lib/riak/search/result_collection.rb', line 31 def initialize(client, raw_results) @client = client @raw = raw_results @max_score = raw['max_score'] @num_found = raw['num_found'] @length = raw['docs'].length end |
Instance Attribute Details
#client ⇒ Riak::Client (readonly)
10 11 12 |
# File 'lib/riak/search/result_collection.rb', line 10 def client @client end |
#length ⇒ Integer (readonly)
Returns the number of documents in this collection.
19 20 21 |
# File 'lib/riak/search/result_collection.rb', line 19 def length @length end |
#max_score ⇒ Numeric (readonly)
Returns the maximum score found by Solr.
16 17 18 |
# File 'lib/riak/search/result_collection.rb', line 16 def max_score @max_score end |
#num_found ⇒ Integer (readonly)
Returns the total number of documents matched, including ones not returned due to row-limiting.
23 24 25 |
# File 'lib/riak/search/result_collection.rb', line 23 def num_found @num_found end |
#raw ⇒ Hash (readonly)
Returns the de-serialzed hash returned from Solr.
13 14 15 |
# File 'lib/riak/search/result_collection.rb', line 13 def raw @raw end |
Instance Method Details
#[](index) ⇒ Riak::RObject, NilClass
Returns the found object, or nil if the index is out of range.
57 58 59 60 61 62 |
# File 'lib/riak/search/result_collection.rb', line 57 def [](index) doc = docs[index] return nil if doc.nil? doc.object end |
#counters ⇒ Array<Riak::Crdt::Counter] counter objects
Materializes [Riak::Crdt::Counter] results.
99 100 101 102 103 |
# File 'lib/riak/search/result_collection.rb', line 99 def counters @counters ||= docs. select{ |d| d.type_class == Riak::Crdt::Counter }. map(&:counter) end |
#crdts ⇒ Array<Riak::Crdt::Base>
Materializes [Riak::Crdt::Base] subclasses from any CRDT results.
92 93 94 |
# File 'lib/riak/search/result_collection.rb', line 92 def crdts @crdts ||= docs.select(&:crdt?).map(&:crdt) end |
#docs ⇒ Array<Riak::Search::ResultDocument>
Access the individual documents from the search results. The document metadata are each wrapped in a Riak::Search::ResultDocument.
43 44 45 46 47 |
# File 'lib/riak/search/result_collection.rb', line 43 def docs @docs ||= raw['docs'].map do |result| ResultDocument.new client, result end end |
#each_robject {|robject| ... } ⇒ Enumerator<Riak::RObject>
Enumerable-compatible iterator method. If a block is given, yields with each RObject in the collection. If no block is given, returns an Enumerator over each {Riak::RObject in the collection.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/riak/search/result_collection.rb', line 128 def each_robject enum = docs.each_with_index if block_given? enum.each do |doc, idx| yield self[idx] end else Enumerator.new do |yielder| enum.each do |doc, idx| yielder << self[idx] end end end end |
#empty? ⇒ Boolean
Returns does this collection contain any documents?.
50 51 52 |
# File 'lib/riak/search/result_collection.rb', line 50 def empty? length == 0 end |
#first ⇒ Riak::RObject, NilClass
Returns the first found object, or nil if the index is out of range.
66 67 68 |
# File 'lib/riak/search/result_collection.rb', line 66 def first self[0] end |
#maps ⇒ Array<Riak::Crdt::Map] map objects
Materializes [Riak::Crdt::Map] results.
108 109 110 111 112 |
# File 'lib/riak/search/result_collection.rb', line 108 def maps @maps ||= docs. select{ |d| d.type_class == Riak::Crdt::Map }. map(&:map) end |
#objects ⇒ Array
Materializes and returns an array of objects from search results. You’ll probably need to type inspect its members.
74 75 76 77 78 79 |
# File 'lib/riak/search/result_collection.rb', line 74 def objects @objects ||= docs.map do |doc| next doc.crdt if doc.crdt? doc.robject end end |
#robjects ⇒ Array<Riak::RObject>
Materializes [Riak::RObject]s from any key-value results. Refuses to return RObjects for any CRDT results.
85 86 87 |
# File 'lib/riak/search/result_collection.rb', line 85 def robjects @robjects ||= docs.reject(&:crdt?).map(&:robject) end |
#sets ⇒ Array<Riak::Crdt::Set>
Materializes [Riak::Crdt::Set] results.
117 118 119 120 121 |
# File 'lib/riak/search/result_collection.rb', line 117 def sets @sets ||= docs. select{ |d| d.type_class == Riak::Crdt::Set }. map(&:set) end |