Skip to content

Commit

Permalink
Merge pull request #443 from menglewis/preference-param
Browse files Browse the repository at this point in the history
Add preference param to Query
  • Loading branch information
pyromaniac authored Oct 6, 2016
2 parents 41f0c7f + 0107b43 commit 7522dc1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Metrics/AbcSize:
# Offense count: 4
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 277
Max: 280

# Offense count: 14
Metrics/CyclomaticComplexity:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

* Add the `track_scores` option to the query; `_score` to be computed and tracked even when there are no `_score` in sort. (@dmitry)

* Added `Query#preference` for specifying shard replicas to query against. (@menglewis)

## Bugfixes

* Fix routing_missing_exception on delete with parent missing (@guigs, #398)
Expand Down
9 changes: 9 additions & 0 deletions lib/chewy/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,15 @@ def decay(function, field, options = {})
chain { criteria.update_scores scoring }
end

# Sets <tt>preference</tt> for request.
# For instance, one can use <tt>preference=_primary</tt> to execute only on the primary shards.
#
# scope = UsersIndex.preference(:_primary)
#
def preference(value)
chain { criteria.update_search_options preference: value }
end

# Sets elasticsearch <tt>aggregations</tt> search request param
#
# UsersIndex.filter{ name == 'Johny' }.aggregations(category_id: {terms: {field: 'category_ids'}})
Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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, :track_scores,
:total_count, :total_entries, :unlimited, :script_fields, :track_scores, :preference,
to: :all
end

Expand Down
8 changes: 8 additions & 0 deletions spec/chewy/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@
end
end

describe '#preference' do
specify { expect(subject.preference(:_primary)).to be_a described_class }
specify { expect(subject.preference(:_primary)).not_to eq(subject) }
specify { expect(subject.preference(:_primary).criteria.search_options).to include(preference: :_primary) }
specify { expect { subject.preference(:_primary) }.not_to change { subject.criteria.search_options } }
specify { expect(subject.preference(:_primary).criteria.request_body).to include(preference: :_primary) }
end

describe '#facets' do
specify do
skip_on_version_lt('2.0')
Expand Down

0 comments on commit 7522dc1

Please sign in to comment.