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.

Since:

  • 6.0.0

Constant Summary collapse

HITS =

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

Since:

  • 6.0.0

'hits'.freeze
TOTAL =

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

Since:

  • 6.0.0

'total'.freeze
VALUE =

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

Since:

  • 6.0.0

'value'.freeze
MAX_SCORE =

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

Since:

  • 6.0.0

'max_score'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Results.

Parameters:

  • repository (Elasticsearch::Persistence::Repository::Class)

    The repository instance

  • response (Hash)

    The full response returned from the Elasticsearch client

  • options (Hash) (defaults to: {})

    Optional parameters

Since:

  • 6.0.0



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

def initialize(repository, response, options={})
  @repository = repository
  @raw_response = response
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Since:

  • 6.0.0



59
60
61
# 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

Instance Attribute Details

#raw_responseObject (readonly)

Since:

  • 6.0.0



31
32
33
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 31

def raw_response
  @raw_response
end

#repositoryObject (readonly)

Since:

  • 6.0.0



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

def repository
  @repository
end

Instance Method Details

#each_with_hit(&block) ⇒ Object

Yields [object, hit] pairs to the block

Since:

  • 6.0.0



85
86
87
# 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) ⇒ Object

Yields [object, hit] pairs and returns the result

Since:

  • 6.0.0



91
92
93
# 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_scoreObject

The maximum score for a query

Since:

  • 6.0.0



79
80
81
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 79

def max_score
  raw_response[HITS][MAX_SCORE]
end

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

Returns:

  • (Boolean)

Since:

  • 6.0.0



63
64
65
# 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

#responseElasticsearch::Model::HashWrapper

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

Returns:

  • (Elasticsearch::Model::HashWrapper)

Since:

  • 6.0.0



121
122
123
# File 'lib/elasticsearch/persistence/repository/response/results.rb', line 121

def response
  @response ||= Elasticsearch::Model::HashWrapper.new(raw_response)
end

#resultsArray

Return the collection of domain objects

Examples:

Iterate over the results


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

Returns:

  • (Array)

Since:

  • 6.0.0



104
105
106
107
108
# 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

#totalObject

The number of total hits for a query

Since:

  • 6.0.0



69
70
71
72
73
74
75
# 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