Skip to content

Commit

Permalink
Add runtime_mappings support
Browse files Browse the repository at this point in the history
  • Loading branch information
TakuyaKurimoto committed May 15, 2023
1 parent 8fe3d0f commit 6a7cf31
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions lib/chewy/search/parameters/runtime_mappings.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 17 additions & 1 deletion lib/chewy/search/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,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: {script: {lang: 'painless', inline: 'some script here'}})
# .runtime_mappings(field2: {script: {lang: 'painless', inline: 'some script here'}})
# # => <PlacesIndex::Query {..., :body=>{: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
Expand Down
5 changes: 5 additions & 0 deletions spec/chewy/search/parameters/runtime_mappings_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/chewy/search/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6a7cf31

Please sign in to comment.