From af46a81d798ccc7e293ff23ce25072f5a8260eb5 Mon Sep 17 00:00:00 2001 From: Matt Zollinhofer Date: Sat, 25 Aug 2018 21:49:17 -0400 Subject: [PATCH] Adding AllowPartialSearchResults parameter Just adding this for completeness. I didn't really test using this so you can definitely remove this if it's not wanted. This support was added in ES 6.3. https://www.elastic.co/blog/this-week-in-elasticsearch-and-apache-lucene-2018-02-05 --- lib/chewy/search/parameters.rb | 2 +- .../allow_partial_search_results.rb | 27 +++++++++++++++++++ spec/chewy/search/parameters_spec.rb | 5 ++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 lib/chewy/search/parameters/allow_partial_search_results.rb diff --git a/lib/chewy/search/parameters.rb b/lib/chewy/search/parameters.rb index f2222eb97..cf845ac3e 100644 --- a/lib/chewy/search/parameters.rb +++ b/lib/chewy/search/parameters.rb @@ -10,7 +10,7 @@ module Search # @see Chewy::Search::Request#parameters # @see Chewy::Search::Parameters::Storage class Parameters - QUERY_STRING_PARAMS = %i[search_type request_cache].freeze + QUERY_STRING_PARAMS = %i[search_type request_cache allow_partial_search_results].freeze # Default storage classes warehouse. It is probably possible to # add your own classes here if necessary, but I'm not sure it will work. # diff --git a/lib/chewy/search/parameters/allow_partial_search_results.rb b/lib/chewy/search/parameters/allow_partial_search_results.rb new file mode 100644 index 000000000..d36e17381 --- /dev/null +++ b/lib/chewy/search/parameters/allow_partial_search_results.rb @@ -0,0 +1,27 @@ +require 'chewy/search/parameters/storage' + +module Chewy + module Search + class Parameters + # Stores boolean value, but has 3 states: `true`, `false` and `nil`. + # + # @see Chewy::Search::Request#allow_partial_search_results + # @see https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-body.html#_parameters_4 + class AllowPartialSearchResults < Storage + # We don't want to render `nil`, but render `true` and `false` values. + # + # @see Chewy::Search::Parameters::Storage#render + # @return [{Symbol => Object}, nil] + def render + {self.class.param_name => value} unless value.nil? + end + + private + + def normalize(value) + !!value unless value.nil? + end + end + end + end +end diff --git a/spec/chewy/search/parameters_spec.rb b/spec/chewy/search/parameters_spec.rb index a4290b80a..1b7859a21 100644 --- a/spec/chewy/search/parameters_spec.rb +++ b/spec/chewy/search/parameters_spec.rb @@ -112,6 +112,11 @@ def storage_object_ids(params) specify { expect(subject.render).to eq(search_type: 'query_then_fetch') } end + context do + subject { described_class.new(allow_partial_search_results: true) } + specify { expect(subject.render).to eq(allow_partial_search_results: true) } + end + context do subject { described_class.new(query: {foo: 'bar'}, filter: {moo: 'baz'}) } specify { expect(subject.render).to eq(body: {query: {bool: {must: {foo: 'bar'}, filter: {moo: 'baz'}}}}) }