Class: Orchestrate::Search::Results

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/orchestrate/search/results.rb

Overview

An enumerator for searching an Orchestrate::Collection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, query, options) ⇒ Results

Initialize a new SearchResults object

Parameters:

  • collection (Orchestrate::Collection)

    The collection searched.

  • query (#to_s)

    The Lucene Query performed.

  • options (Hash)

    The query parameters.


23
24
25
26
27
28
29
# File 'lib/orchestrate/search/results.rb', line 23

def initialize(collection, query, options)
  @collection = collection
  @query = query
  @options = options
  @response = nil
  @aggregates = nil
end

Instance Attribute Details

#aggregates#to_json (readonly)

Returns The response object's aggregate results.

Returns:

  • (#to_json)

    The response object's aggregate results


17
18
19
# File 'lib/orchestrate/search/results.rb', line 17

def aggregates
  @aggregates
end

#collectionCollection (readonly)

Returns The collection this object will search.

Returns:

  • (Collection)

    The collection this object will search.


5
6
7
# File 'lib/orchestrate/search/results.rb', line 5

def collection
  @collection
end

#optionsHash (readonly)

Returns The query paramaters.

Returns:

  • (Hash)

    The query paramaters.


11
12
13
# File 'lib/orchestrate/search/results.rb', line 11

def options
  @options
end

#query#to_s (readonly)

Returns The Lucene Query String given as the search query.

Returns:

  • (#to_s)

    The Lucene Query String given as the search query.


8
9
10
# File 'lib/orchestrate/search/results.rb', line 8

def query
  @query
end

#responseOrchestrate::API::CollectionResponse (readonly)

Returns The response object.

Returns:


14
15
16
# File 'lib/orchestrate/search/results.rb', line 14

def response
  @response
end

Instance Method Details

#eachObject #each {|score,key_value| ... } ⇒ Object

Iterates over each result from the Search. Used as the basis for Enumerable methods. Items are provided on the basis of score, with most relevant first.

Examples:

collection.search("trendy").limit(5).execute.each do |score, item|
  puts "#{item.key} has a trend score of #{score}"
end

Overloads:

  • #eachObject

    Returns Enumerator.

    Returns:

    • Enumerator

  • #each {|score,key_value| ... } ⇒ Object

    Yield Parameters:

Raises:


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/orchestrate/search/results.rb', line 43

def each
  @response ||= collection.perform(:search, query, options)
  @aggregates ||= @response.aggregates
  return enum_for(:each) unless block_given?
  raise Orchestrate::ResultsNotReady.new if collection.app.inside_parallel?
  loop do
    @response.results.each do |listing|
      case listing['path']['kind']
      when 'event'
        kv = collection.stub(listing['key'])
        event_type = Orchestrate::EventType.new(kv, listing['type'])
        event = Orchestrate::Event.from_listing(event_type, listing, @response)
        yield [listing['score'], event]
      else
        yield [listing['score'], Orchestrate::KeyValue.from_listing(collection, listing, @response)]
      end
    end
    break unless @response.next_link
    @response = @response.next_results
  end
  @response = nil
end

#each_aggregateObject #each_aggregate {|| ... } ⇒ Object

Iterates over each aggregate result from the Search. Used as the basis for Enumerable methods. Items are provided on the basis of score, with most relevant first.

Overloads:

  • #each_aggregateObject

    Returns Enumerator.

    Returns:

    • Enumerator

  • #each_aggregate {|| ... } ⇒ Object

    Yield Parameters:

    • ((Orchestrate::Collection::AggregateResult))

Raises:


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/orchestrate/search/results.rb', line 72

def each_aggregate
  @response ||= collection.perform(:search, query, options)
  @aggregates ||= @response.aggregates
  return enum_for(:each_aggregate) unless block_given?
  raise Orchestrate::ResultsNotReady.new if collection.app.inside_parallel?
  @aggregates.each do |listing|
    case listing['aggregate_kind']
    when 'stats'
      yield StatsResult.new(collection, listing)
    when 'range'
      yield RangeResult.new(collection, listing)
    when 'distance'
      yield DistanceResult.new(collection, listing)
    when 'time_series'
      yield TimeSeriesResult.new(collection, listing)
    end
  end
end

#lazyObject

Creates a Lazy Enumerator for the Search Results. If called inside its app's in_parallel block, will pre-fetch results.


93
94
95
96
# File 'lib/orchestrate/search/results.rb', line 93

def lazy
  return each.lazy if collection.app.inside_parallel?
  super
end