Skip to content

Commit

Permalink
Add the track_total_hits option to the query (toptal#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
milk1000cc authored and Çağatay Yücelen committed Jan 28, 2023
1 parent 6ee6b9f commit 2b12781
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### New Features

* [#801](https://github.com/toptal/chewy/pull/801): Add the [`track_total_hits`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-your-data.html#track-total-hits) option to the query. ([@milk1000cc][])

### Changes

### Bugs Fixed
Expand Down Expand Up @@ -674,6 +676,7 @@
[@mattzollinhofer]: https://github.com/mattzollinhofer
[@menglewis]: https://github.com/menglewis
[@mikeyhogarth]: https://github.com/mikeyhogarth
[@milk1000cc]: https://github.com/milk1000cc
[@mkcode]: https://github.com/mkcode
[@mpeychich]: https://github.com/mpeychich
[@mrbrdo]: https://github.com/mrbrdo
Expand Down
16 changes: 16 additions & 0 deletions lib/chewy/search/parameters/track_total_hits.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'chewy/search/parameters/storage'

module Chewy
module Search
class Parameters
# Just a standard boolean storage, nothing to see here.
#
# @see Chewy::Search::Parameters::BoolStorage
# @see Chewy::Search::Request#track_total_hits
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-your-data.html#track-total-hits
class TrackTotalHits < Storage
include BoolStorage
end
end
end
end
17 changes: 15 additions & 2 deletions lib/chewy/search/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Request
EVERFIELDS = %w[_index _type _id _parent].freeze
DELEGATED_METHODS = %i[
query filter post_filter order reorder docvalue_fields
track_scores request_cache explain version profile
track_scores track_total_hits request_cache explain version profile
search_type preference limit offset terminate_after
timeout min_score source stored_fields search_after
load script_fields suggest aggs aggregations none
Expand Down Expand Up @@ -336,6 +336,19 @@ def reorder(value, *values)
# @param value [true, false]
# @return [Chewy::Search::Request]
#
# @!method track_total_hits(value = true)
# Replaces the value of the `track_total_hits` parameter with the provided value.
#
# @example
# PlacesIndex.track_total_hits
# # => <PlacesIndex::Query {..., :body=>{:track_total_hits=>true}}>
# PlacesIndex.track_total_hits.track_total_hits(false)
# # => <PlacesIndex::Query {:index=>["places"]}>
# @see Chewy::Search::Parameters::TrackTotalHits
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-your-data.html#track-total-hits
# @param value [true, false]
# @return [Chewy::Search::Request]
#
# @!method explain(value = true)
# Replaces the value of the `explain` parameter with the provided value.
#
Expand Down Expand Up @@ -388,7 +401,7 @@ def reorder(value, *values)
# @see https://en.wikipedia.org/wiki/Null_Object_pattern
# @param value [true, false]
# @return [Chewy::Search::Request]
%i[track_scores explain version profile none].each do |name|
%i[track_scores track_total_hits explain version profile none].each do |name|
define_method name do |value = true|
modify(name) { replace!(value) }
end
Expand Down
5 changes: 5 additions & 0 deletions spec/chewy/search/parameters/track_total_hits_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'chewy/search/parameters/bool_storage_examples'

describe Chewy::Search::Parameters::TrackTotalHits do
it_behaves_like :bool_storage, :track_total_hits
end
2 changes: 1 addition & 1 deletion spec/chewy/search/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
specify { expect { subject.reorder(:foo) }.not_to change { subject.render } }
end

%i[track_scores explain version profile].each do |name|
%i[track_scores track_total_hits explain version profile].each do |name|
describe "##{name}" do
specify { expect(subject.send(name).render[:body]).to include(name => true) }
specify { expect(subject.send(name).send(name, false).render[:body]).to be_blank }
Expand Down

0 comments on commit 2b12781

Please sign in to comment.