diff --git a/elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb b/elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb index 73fb4f836..79390c92a 100644 --- a/elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb +++ b/elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb @@ -18,7 +18,7 @@ class Results # def initialize(repository, response, options={}) @repository = repository - @response = Elasticsearch::Model::HashWrapper.new(response) + @response = response @options = options end @@ -33,13 +33,13 @@ def respond_to?(method_name, include_private = false) # The number of total hits for a query # def total - response['hits']['total'] + raw_response['hits']['total'] end # The maximum score for a query # def max_score - response['hits']['max_score'] + raw_response['hits']['max_score'] end # Yields [object, hit] pairs to the block @@ -64,7 +64,7 @@ def map_with_hit(&block) # @return [Array] # def results - @results ||= response['hits']['hits'].map do |document| + @results ||= @response['hits']['hits'].map do |document| repository.deserialize(document.to_hash) end end @@ -81,6 +81,21 @@ def results # @return [Elasticsearch::Model::HashWrapper] # def response + @hash_wrapped_response ||= Elasticsearch::Model::HashWrapper.new(@response) + end + + # Access the unwrapped raw 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.raw_response["aggregations"]["titles"]["buckets"].map { |term| "#{term['key']}: #{term['doc_count']}" } + # # => ["brown: 1", "dog: 1", ...] + # + # @return [Hash] + # + def raw_response @response end end diff --git a/elasticsearch-persistence/test/unit/repository_response_results_test.rb b/elasticsearch-persistence/test/unit/repository_response_results_test.rb index df8e08ae5..cd2db3635 100644 --- a/elasticsearch-persistence/test/unit/repository_response_results_test.rb +++ b/elasticsearch-persistence/test/unit/repository_response_results_test.rb @@ -45,6 +45,14 @@ class MyDocument; end assert_equal 5, subject.response._shards.total end + should "provide access to the raw response" do + assert_equal 5, subject.raw_response['_shards']['total'] + end + + should "return the raw response unwrapped" do + assert_equal @response, subject.raw_response + end + should "return the total" do assert_equal 2, subject.total end