From 2b12781cea1ad9d3761a672ad7925ddfbf0aae7c Mon Sep 17 00:00:00 2001 From: milk1000cc Date: Fri, 25 Jun 2021 19:55:17 +0900 Subject: [PATCH] Add the `track_total_hits` option to the query (#801) --- CHANGELOG.md | 3 +++ lib/chewy/search/parameters/track_total_hits.rb | 16 ++++++++++++++++ lib/chewy/search/request.rb | 17 +++++++++++++++-- .../search/parameters/track_total_hits_spec.rb | 5 +++++ spec/chewy/search/request_spec.rb | 2 +- 5 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 lib/chewy/search/parameters/track_total_hits.rb create mode 100644 spec/chewy/search/parameters/track_total_hits_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index ef1e71e42..5d97218b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/lib/chewy/search/parameters/track_total_hits.rb b/lib/chewy/search/parameters/track_total_hits.rb new file mode 100644 index 000000000..dd89ef6c2 --- /dev/null +++ b/lib/chewy/search/parameters/track_total_hits.rb @@ -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 diff --git a/lib/chewy/search/request.rb b/lib/chewy/search/request.rb index b74a56c76..598b2d744 100644 --- a/lib/chewy/search/request.rb +++ b/lib/chewy/search/request.rb @@ -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 @@ -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 + # # => {:track_total_hits=>true}}> + # PlacesIndex.track_total_hits.track_total_hits(false) + # # => ["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. # @@ -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 diff --git a/spec/chewy/search/parameters/track_total_hits_spec.rb b/spec/chewy/search/parameters/track_total_hits_spec.rb new file mode 100644 index 000000000..ea8ecf565 --- /dev/null +++ b/spec/chewy/search/parameters/track_total_hits_spec.rb @@ -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 diff --git a/spec/chewy/search/request_spec.rb b/spec/chewy/search/request_spec.rb index 2ac7790c0..6bc7bc667 100644 --- a/spec/chewy/search/request_spec.rb +++ b/spec/chewy/search/request_spec.rb @@ -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 }