class Elasticsearch::Persistence::Repository::Response::Results

Encapsulates the domain objects and documents returned from Elasticsearch when searching

Implements ‘Enumerable` and forwards its methods to the {#results} object.

Constants

HITS

The key for accessing the results in an Elasticsearch query response.

MAX_SCORE

The key for accessing the maximum score in an Elasticsearch query response.

TOTAL

The key for accessing the total number of hits in an Elasticsearch query response.

VALUE

The key for accessing the value field in an Elasticsearch query response when ‘total’ is an object.

Attributes

raw_response[R]
repository[R]

Public Class Methods

new(repository, response, options={}) click to toggle source

@param repository [Elasticsearch::Persistence::Repository::Class] The repository instance @param response [Hash] The full response returned from the Elasticsearch client @param options [Hash] Optional parameters

# File lib/elasticsearch/persistence/repository/response/results.rb, line 53
def initialize(repository, response, options={})
  @repository = repository
  @raw_response = response
  @options = options
end

Public Instance Methods

each_with_hit(&block) click to toggle source

Yields [object, hit] pairs to the block

# File lib/elasticsearch/persistence/repository/response/results.rb, line 85
def each_with_hit(&block)
  results.zip(raw_response[HITS][HITS]).each(&block)
end
map_with_hit(&block) click to toggle source

Yields [object, hit] pairs and returns the result

# File lib/elasticsearch/persistence/repository/response/results.rb, line 91
def map_with_hit(&block)
  results.zip(raw_response[HITS][HITS]).map(&block)
end
max_score() click to toggle source

The maximum score for a query

# File lib/elasticsearch/persistence/repository/response/results.rb, line 79
def max_score
  raw_response[HITS][MAX_SCORE]
end
method_missing(method_name, *arguments, &block) click to toggle source
Calls superclass method
# File lib/elasticsearch/persistence/repository/response/results.rb, line 59
def method_missing(method_name, *arguments, &block)
  results.respond_to?(method_name) ? results.__send__(method_name, *arguments, &block) : super
end
respond_to?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/elasticsearch/persistence/repository/response/results.rb, line 63
def respond_to?(method_name, include_private = false)
  results.respond_to?(method_name) || super
end
response() click to toggle source

Access the response returned from Elasticsearch by the client

@example Access the aggregations in the response

results = repository.search query: { match: { title: 'fox dog' } },
                            aggregations: { titles: { terms: { field: 'title' } } }
results.response.aggregations.titles.buckets.map { |term| "#{term['key']}: #{term['doc_count']}" }
# => ["brown: 1", "dog: 1", ...]

@return [Elasticsearch::Model::HashWrapper]

# File lib/elasticsearch/persistence/repository/response/results.rb, line 121
def response
  @response ||= Elasticsearch::Model::HashWrapper.new(raw_response)
end
results() click to toggle source

Return the collection of domain objects

@example Iterate over the results

results.map { |r| r.attributes[:title] }
=> ["Fox", "Dog"]

@return [Array]

# File lib/elasticsearch/persistence/repository/response/results.rb, line 104
def results
  @results ||= raw_response[HITS][HITS].map do |document|
    repository.deserialize(document.to_hash)
  end
end
total() click to toggle source

The number of total hits for a query

# File lib/elasticsearch/persistence/repository/response/results.rb, line 69
def total
  if raw_response[HITS][TOTAL].respond_to?(:keys)
    raw_response[HITS][TOTAL][VALUE]
  else
    raw_response[HITS][TOTAL]
  end
end