Skip to content

Avoid unnecessarily incurring the cost of HashWrapper results #755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Results
#
def initialize(repository, response, options={})
@repository = repository
@response = Elasticsearch::Model::HashWrapper.new(response)
@response = response
@options = options
end

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down