Skip to content

Commit 198d1ec

Browse files
committed
[STORE] Lazily wrap response in HashWrapper and provide #raw_response access
1 parent 82ff295 commit 198d1ec

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Results
1111
include Enumerable
1212

1313
attr_reader :repository
14+
attr_reader :raw_response
1415

1516
# The key for accessing the results in an Elasticsearch query response.
1617
#
@@ -30,8 +31,8 @@ class Results
3031
#
3132
def initialize(repository, response, options={})
3233
@repository = repository
33-
@response = Elasticsearch::Model::HashWrapper.new(response)
34-
@options = options
34+
@raw_response = response
35+
@options = options
3536
end
3637

3738
def method_missing(method_name, *arguments, &block)
@@ -45,13 +46,13 @@ def respond_to?(method_name, include_private = false)
4546
# The number of total hits for a query
4647
#
4748
def total
48-
response[HITS][TOTAL]
49+
raw_response[HITS][TOTAL]
4950
end
5051

5152
# The maximum score for a query
5253
#
5354
def max_score
54-
response[HITS][MAX_SCORE]
55+
raw_response[HITS][MAX_SCORE]
5556
end
5657

5758
# Yields [object, hit] pairs to the block
@@ -76,7 +77,7 @@ def map_with_hit(&block)
7677
# @return [Array]
7778
#
7879
def results
79-
@results ||= response[HITS][HITS].map do |document|
80+
@results ||= raw_response[HITS][HITS].map do |document|
8081
repository.deserialize(document.to_hash)
8182
end
8283
end
@@ -93,7 +94,7 @@ def results
9394
# @return [Elasticsearch::Model::HashWrapper]
9495
#
9596
def response
96-
@response
97+
@response ||= Elasticsearch::Model::HashWrapper.new(raw_response)
9798
end
9899
end
99100
end

elasticsearch-persistence/test/unit/repository_response_results_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class MyDocument; end
9393

9494
assert_equal ['FOO---bar'], subject.map_with_hit { |object, hit| "#{object}---#{hit.foo}" }
9595
end
96+
97+
should "provide access to the raw response" do
98+
assert_equal 5, subject.raw_response['_shards']['total']
99+
end
96100
end
97101

98102
end

0 commit comments

Comments
 (0)