From ae9ccca366468530a712693166148629fa5d0a19 Mon Sep 17 00:00:00 2001 From: Dmitry Polushkin Date: Wed, 24 Aug 2016 16:36:58 +0100 Subject: [PATCH 1/3] Add the `track_scores` option to the query `_score` to be computed and tracked even when there are no `_score` in sort. --- CHANGELOG.md | 2 ++ lib/chewy/query.rb | 8 ++++++++ lib/chewy/search.rb | 3 ++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02173e68d..a03329178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ * Rake tasks support multiple indexes and exceptions: `rake chewy:reset[users,projects]`, `rake chewy:update[-projects]` * Witchcraft™ supports dynamically generated procs with variables from closure. + + * Add the `track_scores` option to the query; `_score` to be computed and tracked even when there are no `_score` in sort. (@dmitry) ## Bugfixes diff --git a/lib/chewy/query.rb b/lib/chewy/query.rb index 3d76cf864..a3edd326e 100644 --- a/lib/chewy/query.rb +++ b/lib/chewy/query.rb @@ -325,6 +325,14 @@ def min_score(value) chain { criteria.update_request_options min_score: value } end + # Elasticsearch track_scores option support + # + # UsersIndex.query(...).track_scores(true) + # + def track_scores value + chain { criteria.update_request_options track_scores: value } + end + # Adds facets section to the search request. # All the chained facets a merged and added to the # search request diff --git a/lib/chewy/search.rb b/lib/chewy/search.rb index 4d7ccbaa0..9759f5adb 100644 --- a/lib/chewy/search.rb +++ b/lib/chewy/search.rb @@ -10,7 +10,8 @@ module Search :boost_factor, :weight, :random_score, :field_value_factor, :decay, :aggregations, :suggest, :none, :strategy, :query, :filter, :post_filter, :boost_mode, :score_mode, :order, :reorder, :only, :types, :delete_all, :find, :total, - :total_count, :total_entries, :unlimited, :script_fields, to: :all + :total_count, :total_entries, :unlimited, :script_fields, :track_scores, + to: :all end module ClassMethods From 37cd7f417bc17f1c79e151f281887029b673e1ab Mon Sep 17 00:00:00 2001 From: Dmitry Polushkin Date: Thu, 15 Sep 2016 15:14:05 +0100 Subject: [PATCH 2/3] Add tests for track_scores --- spec/chewy/query_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/chewy/query_spec.rb b/spec/chewy/query_spec.rb index cdd335df3..ba35cd119 100644 --- a/spec/chewy/query_spec.rb +++ b/spec/chewy/query_spec.rb @@ -107,6 +107,14 @@ specify { expect(subject.offset { 20 / 2 }.criteria.request_body[:body]).to include(from: 10) } end + describe '#track_scores' do + specify { expect(subject.track_scores(true)).to be_a described_class } + specify { expect(subject.track_scores(true)).not_to eq(subject) } + specify { expect(subject.track_scores(true).criteria.request_options).to include(track_scores: true) } + specify { expect { subject.track_scores(true) }.not_to change { subject.criteria.request_options } } + specify { expect(subject.track_scores(false).criteria.request_body[:body]).to include(track_scores: false) } + end + describe '#script_fields' do specify { expect(subject.script_fields(distance: 'test()')).to be_a described_class } specify { expect(subject.script_fields(distance: 'test()')).not_to eq(subject) } From 54c5c026db75ec1e2538b9ed13d93beeede3f63b Mon Sep 17 00:00:00 2001 From: Dmitry Polushkin Date: Tue, 27 Sep 2016 18:08:11 +0100 Subject: [PATCH 3/3] Fix rubocop --- .rubocop_todo.yml | 2 +- lib/chewy/query.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a39e99b2d..b5de8abde 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -13,7 +13,7 @@ Metrics/AbcSize: # Offense count: 4 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 274 + Max: 277 # Offense count: 14 Metrics/CyclomaticComplexity: diff --git a/lib/chewy/query.rb b/lib/chewy/query.rb index a3edd326e..55694395c 100644 --- a/lib/chewy/query.rb +++ b/lib/chewy/query.rb @@ -329,7 +329,7 @@ def min_score(value) # # UsersIndex.query(...).track_scores(true) # - def track_scores value + def track_scores(value) chain { criteria.update_request_options track_scores: value } end