Class: Elasticsearch::Persistence::Repository::Response::Results

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/elasticsearch/persistence/repository/response/results.rb

Overview

Encapsulates the domain objects and documents returned from Elasticsearch when searching

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, response, options = {}) ⇒ Results



19
20
21
22
23
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 19

def initialize(repository, response, options={})
  @repository = repository
  @response   = Hashie::Mash.new(response)
  @options    = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



25
26
27
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 25

def method_missing(method_name, *arguments, &block)
  results.respond_to?(method_name) ? results.__send__(method_name, *arguments, &block) : super
end

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository.



13
14
15
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 13

def repository
  @repository
end

Instance Method Details

#each_with_hit(&block) ⇒ Object

Yields [object, hit] pairs to the block



47
48
49
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 47

def each_with_hit(&block)
  results.zip(response['hits']['hits']).each(&block)
end

#map_with_hit(&block) ⇒ Object

Yields [object, hit] pairs and returns the result



53
54
55
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 53

def map_with_hit(&block)
  results.zip(response['hits']['hits']).map(&block)
end

#max_scoreObject

The maximum score for a query



41
42
43
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 41

def max_score
  response['hits']['max_score']
end

#respond_to?(method_name, include_private = false) ⇒ Boolean



29
30
31
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 29

def respond_to?(method_name, include_private = false)
  results.respond_to?(method_name) || super
end

#responseHashie::Mash

Access the response returned from Elasticsearch by the client

Examples:

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", ...]


83
84
85
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 83

def response
  @response
end

#resultsArray

Return the collection of domain objects

Examples:

Iterate over the results


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


66
67
68
69
70
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 66

def results
  @results ||= response['hits']['hits'].map do |document|
    repository.deserialize(document.to_hash)
  end
end

#totalObject

The number of total hits for a query



35
36
37
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 35

def total
  response['hits']['total']
end