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.
Attributes
Public Class Methods
@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 22 def initialize(repository, response, options={}) @repository = repository @response = Elasticsearch::Persistence::Model::HashWrapper.new(response) @options = options @loaded = false end
Public Instance Methods
# File lib/elasticsearch/persistence/repository/response/results.rb, line 83 def delete(opts=nil) end
Yields [object, hit] pairs to the block
# File lib/elasticsearch/persistence/repository/response/results.rb, line 56 def each_with_hit(&block) results.zip(response['hits']['hits']).each(&block) end
# File lib/elasticsearch/persistence/repository/response/results.rb, line 37 def inner_hits response['hits']['hits'].collect { |d| d['inner_hits'] } end
Yields [object, hit] pairs and returns the result
# File lib/elasticsearch/persistence/repository/response/results.rb, line 62 def map_with_hit(&block) results.zip(response['hits']['hits']).map(&block) end
The maximum score for a query
# File lib/elasticsearch/persistence/repository/response/results.rb, line 50 def max_score response['hits']['max_score'] end
# File lib/elasticsearch/persistence/repository/response/results.rb, line 29 def method_missing(method_name, *arguments, &block) results.respond_to?(method_name) ? results.__send__(method_name, *arguments, &block) : super end
# File lib/elasticsearch/persistence/repository/response/results.rb, line 33 def respond_to?(method_name, include_private = false) results.respond_to?(method_name) || super end
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 [Hashie::Mash]
# File lib/elasticsearch/persistence/repository/response/results.rb, line 97 def response @response end
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 75 def results @results ||= response['hits']['hits'].map do |document| repository.deserialize(document.to_hash) end @loaded = true @results end
The number of total hits for a query
# File lib/elasticsearch/persistence/repository/response/results.rb, line 44 def total response['hits']['total'] end