From 693f689e73a0d28d5dda859e0aa834fe7e10e4be Mon Sep 17 00:00:00 2001 From: takuyaK Date: Mon, 15 May 2023 19:45:32 +0900 Subject: [PATCH] Add runtime_mappings support --- CHANGELOG.md | 1 + README.md | 17 ++++++++++++++++ .../search/parameters/runtime_mappings.rb | 14 +++++++++++++ lib/chewy/search/request.rb | 20 +++++++++++++++++-- .../parameters/runtime_mappings_spec.rb | 5 +++++ spec/chewy/search/request_spec.rb | 2 +- 6 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 lib/chewy/search/parameters/runtime_mappings.rb create mode 100644 spec/chewy/search/parameters/runtime_mappings_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c27c85fa..ee5230a16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### New Features +* [#887](https://github.com/toptal/chewy/pull/887): Add support [runtime_mappings](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-search-request.html). ([@TakuyaKurimoto](https://github.com/TakuyaKurimoto)) ### Changes ### Bugs Fixed diff --git a/README.md b/README.md index 096bdb578..55e7bc911 100644 --- a/README.md +++ b/README.md @@ -1171,6 +1171,23 @@ scope.each do |wrapper| end ``` +#### runtime_mappings + +You can define runtime fields in a search request as described [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-search-request.html): + +```ruby +CitiesIndex + .runtime_mappings( + field1: { + type: 'keyword', + :script=>{ + :lang=>"painless", + :source=>"emit('some script here')" + } + } + ) +``` + ### Rake tasks For a Rails application, some index-maintaining rake tasks are defined. diff --git a/lib/chewy/search/parameters/runtime_mappings.rb b/lib/chewy/search/parameters/runtime_mappings.rb new file mode 100644 index 000000000..b60a09a6f --- /dev/null +++ b/lib/chewy/search/parameters/runtime_mappings.rb @@ -0,0 +1,14 @@ +module Chewy + module Search + class Parameters + # Just a standard hash storage. Nothing to see here. + # + # @see Chewy::Search::Parameters::HashStorage + # @see Chewy::Search::Request#runtime_mappings + # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-search-request.html + class RuntimeMappings < Storage + include HashStorage + end + end + end +end diff --git a/lib/chewy/search/request.rb b/lib/chewy/search/request.rb index 27d5cdefe..1254888a9 100644 --- a/lib/chewy/search/request.rb +++ b/lib/chewy/search/request.rb @@ -25,7 +25,7 @@ class Request search_type preference limit offset terminate_after timeout min_score source stored_fields search_after load script_fields suggest aggs aggregations collapse none - indices_boost rescore highlight total total_count + indices_boost rescore highlight runtime_mappings total total_count total_entries indices types delete_all count exists? exist? find pluck scroll_batches scroll_hits scroll_results scroll_wrappers ignore_unavailable @@ -656,7 +656,23 @@ def load(options = nil) # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html # @param value [Hash] # @return [Chewy::Search::Request] - %i[script_fields indices_boost rescore highlight].each do |name| + # + # @!method runtime_mappings(value) + # Add a `runtime_mappings` part to the request. Further + # call values are merged to the storage hash. + # + # @example + # PlacesIndex + # .runtime_mappings(field1: {type: "keyword", :script=>{:lang=>"painless", :source=>"emit('some script here')"}}) + # .runtime_mappings(field2: {type: "keyword", :script=>{:lang=>"painless", :source=>"emit('some script here')"}}) + # # => {:runtime_mappings=>{ + # # "field1"=>{:type=>"keyword", :script=>{:lang=>"painless", :source=>"emit('some script here')"}}, + # # "field2"=>{:type=>"keyword", :script=>{:lang=>"painless", :source=>"emit('some script here')"}}}}}> + # @see Chewy::Search::Parameters::ScriptFields + # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-search-request.html + # @param value [Hash] + # @return [Chewy::Search::Request] + %i[script_fields indices_boost rescore highlight runtime_mappings].each do |name| define_method name do |value| modify(name) { update!(value) } end diff --git a/spec/chewy/search/parameters/runtime_mappings_spec.rb b/spec/chewy/search/parameters/runtime_mappings_spec.rb new file mode 100644 index 000000000..f33347393 --- /dev/null +++ b/spec/chewy/search/parameters/runtime_mappings_spec.rb @@ -0,0 +1,5 @@ +require 'chewy/search/parameters/hash_storage_examples' + +describe Chewy::Search::Parameters::RuntimeMappings do + it_behaves_like :hash_storage, :runtime_mappings +end diff --git a/spec/chewy/search/request_spec.rb b/spec/chewy/search/request_spec.rb index b73b99161..af004bac7 100644 --- a/spec/chewy/search/request_spec.rb +++ b/spec/chewy/search/request_spec.rb @@ -284,7 +284,7 @@ specify { expect { subject.stored_fields(:foo) }.not_to change { subject.render } } end - %i[script_fields highlight].each do |name| + %i[script_fields highlight runtime_mappings].each do |name| describe "##{name}" do specify { expect(subject.send(name, foo: {bar: 42}).render[:body]).to include(name => {'foo' => {bar: 42}}) } specify do