Class: Riak::Search::ResultCollection

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

Instance Method Summary collapse

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

#clientRiak::Client (readonly)

Returns:


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

def client
  @client
end

#lengthInteger (readonly)

Returns the number of documents in this collection

Returns:

  • (Integer)

    the number of documents in this collection


19
20
21
# File 'lib/riak/search/result_collection.rb', line 19

def length
  @length
end

#max_scoreNumeric (readonly)

Returns the maximum score found by Solr

Returns:

  • (Numeric)

    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_foundInteger (readonly)

Returns the total number of documents matched, including ones not returned due to row-limiting

Returns:

  • (Integer)

    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

#rawHash (readonly)

Returns the de-serialzed hash returned from Solr

Returns:

  • (Hash)

    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

Parameters:

  • index (Integer)

    the index of the [Riak::RObject] to load and return

Returns:


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

#countersArray<Riak::Crdt::Counter] counter objects

Materializes [Riak::Crdt::Counter] results.

Returns:


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

#crdtsArray<Riak::Crdt::Base>

Materializes [Riak::Crdt::Base] subclasses from any CRDT results.

Returns:


92
93
94
# File 'lib/riak/search/result_collection.rb', line 92

def crdts
  @crdts ||= docs.select(&:crdt?).map(&:crdt)
end

#docsArray<Riak::Search::ResultDocument>

Access the individual documents from the search results. The document metadata are each wrapped in a Riak::Search::ResultDocument.

Returns:


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.

Yield Parameters:

Returns:


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?

Returns:

  • (Boolean)

    does this collection contain any documents?


50
51
52
# File 'lib/riak/search/result_collection.rb', line 50

def empty?
  length == 0
end

#firstRiak::RObject, NilClass

Returns the first found object, or nil if the index is out of range

Returns:


66
67
68
# File 'lib/riak/search/result_collection.rb', line 66

def first
  self[0]
end

#mapsArray<Riak::Crdt::Map] map objects

Materializes [Riak::Crdt::Map] results.

Returns:


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

#objectsArray

Materializes and returns an array of objects from search results. You'll probably need to type inspect its members.

Returns:

  • (Array)

    materialized objects


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

#robjectsArray<Riak::RObject>

Materializes [Riak::RObject]s from any key-value results. Refuses to return RObjects for any CRDT results.

Returns:


85
86
87
# File 'lib/riak/search/result_collection.rb', line 85

def robjects
  @robjects ||= docs.reject(&:crdt?).map(&:robject)
end

#setsArray<Riak::Crdt::Set>

Materializes [Riak::Crdt::Set] results.

Returns:


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