From 3fd0d73c883488632d04a0f2ce6e2f3aa8a5da2e Mon Sep 17 00:00:00 2001 From: Emily Stolfo Date: Mon, 13 Aug 2018 14:57:16 +0200 Subject: [PATCH 1/4] [STORE] Lazily wrap response in HashWrapper and provide #raw_response access --- .../repository/response/results.rb | 13 ++++++----- .../spec/repository/response/results_spec.rb | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb b/elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb index 169ecd42e..721e25388 100644 --- a/elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb +++ b/elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb @@ -11,6 +11,7 @@ class Results include Enumerable attr_reader :repository + attr_reader :raw_response # The key for accessing the results in an Elasticsearch query response. # @@ -30,8 +31,8 @@ class Results # def initialize(repository, response, options={}) @repository = repository - @response = Elasticsearch::Model::HashWrapper.new(response) - @options = options + @raw_response = response + @options = options end def method_missing(method_name, *arguments, &block) @@ -45,13 +46,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 @@ -76,7 +77,7 @@ def map_with_hit(&block) # @return [Array] # def results - @results ||= response[HITS][HITS].map do |document| + @results ||= raw_response[HITS][HITS].map do |document| repository.deserialize(document.to_hash) end end @@ -93,7 +94,7 @@ def results # @return [Elasticsearch::Model::HashWrapper] # def response - @response + @response ||= Elasticsearch::Model::HashWrapper.new(raw_response) end end end diff --git a/elasticsearch-persistence/spec/repository/response/results_spec.rb b/elasticsearch-persistence/spec/repository/response/results_spec.rb index 66a2cc6c0..f41f4b81d 100644 --- a/elasticsearch-persistence/spec/repository/response/results_spec.rb +++ b/elasticsearch-persistence/spec/repository/response/results_spec.rb @@ -66,6 +66,22 @@ def deserialize(document) it 'wraps the response in a HashWrapper' do expect(results.response._shards.total).to eq(5) end + + context 'when the response method is not called' do + + it 'does not create an instance of HashWrapper' do + expect(Elasticsearch::Model::HashWrapper).not_to receive(:new) + results + end + end + + context 'when the response method is called' do + + it 'does create an instance of HashWrapper' do + expect(Elasticsearch::Model::HashWrapper).to receive(:new) + results.response + end + end end describe '#total' do @@ -102,4 +118,11 @@ def deserialize(document) expect(results.map_with_hit { |pair| pair[0] }).to eq(['Object', 'Object']) end end + + describe '#raw_response' do + + it 'returns the raw response from Elasticsearch' do + expect(results.raw_response).to eq(response) + end + end end From a6bf55699987c0e2965ae09fa6fb6802d1c16370 Mon Sep 17 00:00:00 2001 From: Emily Stolfo Date: Mon, 13 Aug 2018 14:57:47 +0200 Subject: [PATCH 2/4] [MODEL] Lazily wrap response in HashWrapper and provide #raw_response access --- .../lib/elasticsearch/model/response.rb | 10 ++++++---- .../lib/elasticsearch/model/response/base.rb | 1 + elasticsearch-model/test/unit/response_results_test.rb | 3 +++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/elasticsearch-model/lib/elasticsearch/model/response.rb b/elasticsearch-model/lib/elasticsearch/model/response.rb index d15c24a04..e350957c7 100644 --- a/elasticsearch-model/lib/elasticsearch/model/response.rb +++ b/elasticsearch-model/lib/elasticsearch/model/response.rb @@ -27,9 +27,7 @@ def initialize(klass, search, options={}) # @return [Hash] # def response - @response ||= begin - HashWrapper.new(search.execute!) - end + @response ||= HashWrapper.new(search.execute!) end # Returns the collection of "hits" from Elasticsearch @@ -63,7 +61,7 @@ def timed_out # Returns the statistics on shards # def shards - HashWrapper.new(response['_shards']) + response['_shards'] end # Returns a Hashie::Mash of the aggregations @@ -77,6 +75,10 @@ def aggregations def suggestions Suggestions.new(response['suggest']) end + + def raw_response + @raw_response ||= search.execute! + end end end end diff --git a/elasticsearch-model/lib/elasticsearch/model/response/base.rb b/elasticsearch-model/lib/elasticsearch/model/response/base.rb index 3bb8005b6..3d5e77ff3 100644 --- a/elasticsearch-model/lib/elasticsearch/model/response/base.rb +++ b/elasticsearch-model/lib/elasticsearch/model/response/base.rb @@ -13,6 +13,7 @@ module Base def initialize(klass, response, options={}) @klass = klass @response = response + @raw_response = response end # @abstract Implement this method in specific class diff --git a/elasticsearch-model/test/unit/response_results_test.rb b/elasticsearch-model/test/unit/response_results_test.rb index e97539ecd..900094bf5 100644 --- a/elasticsearch-model/test/unit/response_results_test.rb +++ b/elasticsearch-model/test/unit/response_results_test.rb @@ -27,5 +27,8 @@ def self.document_type; 'bar'; end assert_equal 'bar', @results.first.foo end + should "provide access to the raw response" do + assert_equal RESPONSE, @response.raw_response + end end end From 25d73339739da965d10b5432a9cc16403f8e951f Mon Sep 17 00:00:00 2001 From: Emily Stolfo Date: Wed, 5 Sep 2018 14:23:19 +0200 Subject: [PATCH 3/4] [MODEL] Opimitize response changes --- .../lib/elasticsearch/model/response.rb | 13 ++++++------- .../lib/elasticsearch/model/response/base.rb | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/elasticsearch-model/lib/elasticsearch/model/response.rb b/elasticsearch-model/lib/elasticsearch/model/response.rb index e350957c7..9726ae2c5 100644 --- a/elasticsearch-model/lib/elasticsearch/model/response.rb +++ b/elasticsearch-model/lib/elasticsearch/model/response.rb @@ -10,8 +10,7 @@ module Response # Implements Enumerable and forwards its methods to the {#results} object. # class Response - attr_reader :klass, :search, :response, - :took, :timed_out, :shards + attr_reader :klass, :search include Enumerable @@ -49,31 +48,31 @@ def records(options = {}) # Returns the "took" time # def took - response['took'] + raw_response['took'] end # Returns whether the response timed out # def timed_out - response['timed_out'] + raw_response['timed_out'] end # Returns the statistics on shards # def shards - response['_shards'] + @shards ||= HashWrapper.new(raw_response['_shards']) end # Returns a Hashie::Mash of the aggregations # def aggregations - Aggregations.new(response['aggregations']) + @aggregations ||= Aggregations.new(raw_response['aggregations']) end # Returns a Hashie::Mash of the suggestions # def suggestions - Suggestions.new(response['suggest']) + @suggestions ||= Suggestions.new(raw_response['suggest']) end def raw_response diff --git a/elasticsearch-model/lib/elasticsearch/model/response/base.rb b/elasticsearch-model/lib/elasticsearch/model/response/base.rb index 3d5e77ff3..827c52e35 100644 --- a/elasticsearch-model/lib/elasticsearch/model/response/base.rb +++ b/elasticsearch-model/lib/elasticsearch/model/response/base.rb @@ -4,7 +4,7 @@ module Response # Common funtionality for classes in the {Elasticsearch::Model::Response} module # module Base - attr_reader :klass, :response + attr_reader :klass, :response, :raw_response # @param klass [Class] The name of the model class # @param response [Hash] The full response returned from Elasticsearch client @@ -12,8 +12,8 @@ module Base # def initialize(klass, response, options={}) @klass = klass - @response = response @raw_response = response + @response = response end # @abstract Implement this method in specific class From 5c704dac9b2e1a9eb2d953f75779955e7f30061a Mon Sep 17 00:00:00 2001 From: Emily Stolfo Date: Wed, 5 Sep 2018 14:23:54 +0200 Subject: [PATCH 4/4] [STORE] Opimitize response changes --- .../elasticsearch/persistence/repository/response/results.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb b/elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb index 721e25388..9de0059f3 100644 --- a/elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb +++ b/elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb @@ -58,13 +58,13 @@ def max_score # Yields [object, hit] pairs to the block # def each_with_hit(&block) - results.zip(response[HITS][HITS]).each(&block) + results.zip(raw_response[HITS][HITS]).each(&block) end # Yields [object, hit] pairs and returns the result # def map_with_hit(&block) - results.zip(response[HITS][HITS]).map(&block) + results.zip(raw_response[HITS][HITS]).map(&block) end # Return the collection of domain objects