diff --git a/package.json b/package.json index 3df0fdd..1e2ff23 100644 --- a/package.json +++ b/package.json @@ -107,6 +107,7 @@ "kennylindahl ", "foxstarius ", "sandeep952 ", - "florian-lackner365 " + "florian-lackner365 ", + "Alejandro Marulanda " ] } diff --git a/src/core/runtime-field.js b/src/core/runtime-field.js index 5820d0b..570fc41 100644 --- a/src/core/runtime-field.js +++ b/src/core/runtime-field.js @@ -46,19 +46,20 @@ class RuntimeField { /** * Sets the source of the script. * @param {string} script - * @returns {void} + * @returns {RuntimeField} returns `this` so that calls can be chained. */ script(script) { this._body.script = { source: script }; this._isScriptSet = true; + return this; } /** * Sets the type of the runtime field. * @param {string} type One of `boolean`, `composite`, `date`, `double`, `geo_point`, `ip`, `keyword`, `long`, `lookup`. - * @returns {void} + * @returns {RuntimeField} returns `this` so that calls can be chained. */ type(type) { const typeLower = type.toLowerCase(); @@ -67,6 +68,34 @@ class RuntimeField { } this._body.type = typeLower; this._isTypeSet = true; + return this; + } + + /** + * Specifies the language the script is written in. Defaults to `painless` but + * may be set to any of languages listed in [Scripting](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html). + * + * @param {string} lang The language for the script. + * @returns {RuntimeField} returns `this` so that calls can be chained. + */ + lang(lang) { + if (!isNil(this._body.script)) { + this._body.script.lang = lang; + } + return this; + } + + /** + * Specifies any named parameters that are passed into the script as variables. + * + * @param {Object} params Named parameters to be passed to script. + * @returns {RuntimeField} returns `this` so that calls can be chained. + */ + params(params) { + if (!isNil(this._body.script)) { + this._body.script.params = params; + } + return this; } /** diff --git a/src/index.d.ts b/src/index.d.ts index c14842e..45c46a9 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -9009,17 +9009,34 @@ declare namespace esb { * Sets the type of the runtime field. * * @param {string} type One of `boolean`, `composite`, `date`, `double`, `geo_point`, `ip`, `keyword`, `long`, `lookup`. - * @returns {void} + * @returns {RuntimeField} returns `this` so that calls can be chained. */ - type(type: 'boolean' | 'composite' | 'date' | 'double' | 'geo_point' | 'ip' | 'keyword' | 'long' | 'lookup'): void; + type(type: 'boolean' | 'composite' | 'date' | 'double' | 'geo_point' | 'ip' | 'keyword' | 'long' | 'lookup'): this; /** * Sets the source of the script. * * @param {string} script - * @returns {void} + * @returns {RuntimeField} returns `this` so that calls can be chained. */ - script(script: string): void; + script(script: string): this; + + /** + * Specifies the language the script is written in. Defaults to `painless` but + * may be set to any of languages listed in [Scripting](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html). + * + * @param {string} lang The language for the script. + * @returns {RuntimeField} returns `this` so that calls can be chained. + */ + lang(lang: string): this; + + /** + * Specifies any named parameters that are passed into the script as variables. + * + * @param {object} params Named parameters to be passed to script. + * @returns {RuntimeField} returns `this` so that calls can be chained. + */ + params(params: object): this; /** * Override default `toJSON` to return DSL representation for the `script`. diff --git a/test/core-test/request-body-search.test.js b/test/core-test/request-body-search.test.js index 2327606..482afee 100644 --- a/test/core-test/request-body-search.test.js +++ b/test/core-test/request-body-search.test.js @@ -45,6 +45,12 @@ const runtimeFieldB = new RuntimeField( 'boolean', "emit(doc['qty'].value > 10)" ); +const runtimeFieldC = new RuntimeField( + 'keyword', + "emit(doc['my_field_name'].value * params.factor)" +) + .lang('painless') + .params({ factor: 2.0 }); const scriptA = new Script('inline', "doc['my_field_name'].value * 2").lang( 'painless' @@ -178,6 +184,22 @@ test(setsOption, 'runtimeMappings', { }, keyName: 'runtime_mappings' }); +test('Runtime mappging with lang and params', setsOption, 'runtimeMapping', { + param: ['test1', runtimeFieldC], + propValue: { + test1: { + type: 'keyword', + script: { + lang: 'painless', + source: "emit(doc['my_field_name'].value * params.factor)", + params: { + factor: 2.0 + } + } + } + }, + keyName: 'runtime_mappings' +}); test(setsOption, 'scriptField', { param: ['test1', scriptA], diff --git a/test/core-test/runtime-field.test.js b/test/core-test/runtime-field.test.js index 4d487bb..610c539 100644 --- a/test/core-test/runtime-field.test.js +++ b/test/core-test/runtime-field.test.js @@ -51,3 +51,29 @@ test('script method sets script source', t => { }; t.deepEqual(fieldA.toJSON(), expected); }); + +test('set script, lang and params', t => { + const fieldA = new RuntimeField('keyword'); + fieldA.script("emit(doc['my_field_name'].value * params.factor)"); + fieldA.lang('painless'); + fieldA.params({ factor: 2.0 }); + const expected = { + type: 'keyword', + script: { + lang: 'painless', + source: "emit(doc['my_field_name'].value * params.factor)", + params: { + factor: 2.0 + } + } + }; + t.deepEqual(fieldA.toJSON(), expected); +}); + +test("don't set lang and params if script is not set", t => { + const fieldA = new RuntimeField('keyword'); + fieldA.lang('painless'); + fieldA.params({ factor: 2.0 }); + const error = t.throws(() => fieldA.toJSON()); + t.is(error.message, '`script` should be set'); +});