class Riak::Search::ResultCollection
A collection of Riak
Search
2 (“Yokozuna”) results. Provides direct access to the {Riak::RObject} instances found, and access through the docs
method to the results as returned by Solr.
Attributes
@return [Riak::Client]
@return [Integer] the number of documents in this collection
@return [Numeric] the maximum score found by Solr
@return [Integer] the total number of documents matched, including ones
not returned due to row-limiting
@return [Hash] the de-serialzed hash returned from Solr
Public Class Methods
Initialize a {ResultCollection} with the client queried and the raw JSON
returned from the search API.
This is automatically called by {Riak::Search::Query}#results
@api private
# 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
Public Instance Methods
@param [Integer] index the index of the [Riak::RObject] to load and return @return [Riak::RObject,NilClass] the found object, or nil if the index
is out of range
# File lib/riak/search/result_collection.rb, line 57 def [](index) doc = docs[index] return nil if doc.nil? doc.object end
Materializes [Riak::Crdt::Counter] results.
@return [Array<Riak::Crdt::Counter] counter objects
# File lib/riak/search/result_collection.rb, line 99 def counters @counters ||= docs. select{ |d| d.type_class == Riak::Crdt::Counter }. map(&:counter) end
Materializes [Riak::Crdt::Base] subclasses from any CRDT results.
@return [Array<Riak::Crdt::Base>] CRDT objects
# File lib/riak/search/result_collection.rb, line 92 def crdts @crdts ||= docs.select(&:crdt?).map(&:crdt) end
Access the individual documents from the search results. The document metadata are each wrapped in a {Riak::Search::ResultDocument}.
@return [Array<Riak::Search::ResultDocument>] individual documents
# File lib/riak/search/result_collection.rb, line 43 def docs @docs ||= raw['docs'].map do |result| ResultDocument.new client, result end end
{Enumerable}-compatible iterator method. If a block is given, yields with each {Riak::RObject} in the collection. If no block is given, returns an {Enumerator} over each {Riak::RObject in the collection. @yieldparam robject [Riak::RObject] @return [Enumerator<Riak::RObject>]
# 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
@return [Boolean] does this collection contain any documents?
# File lib/riak/search/result_collection.rb, line 50 def empty? length == 0 end
@return [Riak::RObject,NilClass] the first found object, or nil if the
index is out of range
# File lib/riak/search/result_collection.rb, line 66 def first self[0] end
Materializes [Riak::Crdt::Map] results.
@return [Array<Riak::Crdt::Map] map objects
# File lib/riak/search/result_collection.rb, line 108 def maps @maps ||= docs. select{ |d| d.type_class == Riak::Crdt::Map }. map(&:map) end
Materializes and returns an array of objects from search results. You’ll probably need to type inspect its members.
@return [Array] materialized objects
# 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
Materializes [Riak::RObject]s from any key-value results. Refuses to return RObjects for any CRDT results.
@return [Array<Riak::RObject>] key-value objects
# File lib/riak/search/result_collection.rb, line 85 def robjects @robjects ||= docs.reject(&:crdt?).map(&:robject) end
Materializes [Riak::Crdt::Set] results.
@return [Array<Riak::Crdt::Set>]
# File lib/riak/search/result_collection.rb, line 117 def sets @sets ||= docs. select{ |d| d.type_class == Riak::Crdt::Set }. map(&:set) end