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

client[R]

@return [Riak::Client]

length[R]

@return [Integer] the number of documents in this collection

max_score[R]

@return [Numeric] the maximum score found by Solr

num_found[R]

@return [Integer] the total number of documents matched, including ones

not returned due to row-limiting
raw[R]

@return [Hash] the de-serialzed hash returned from Solr

Public Class Methods

new(client, raw_results) click to toggle source

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

[](index) click to toggle source

@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
counters() click to toggle source

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
crdts() click to toggle source

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
docs() click to toggle source

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
each_robject() { |self| ... } click to toggle source

{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
empty?() click to toggle source

@return [Boolean] does this collection contain any documents?

# File lib/riak/search/result_collection.rb, line 50
def empty?
  length == 0
end
first() click to toggle source

@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
maps() click to toggle source

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
objects() click to toggle source

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
robjects() click to toggle source

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
sets() click to toggle source

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