Skip to content

Commit 7150119

Browse files
alejokfAlejandro Marulanda
andauthored
feat: Runtime field script params (#202)
Co-authored-by: Alejandro Marulanda <[email protected]>
1 parent 78de179 commit 7150119

File tree

5 files changed

+102
-7
lines changed

5 files changed

+102
-7
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
"kennylindahl <[email protected]>",
108108
"foxstarius <[email protected]>",
109109
"sandeep952 <[email protected]>",
110-
"florian-lackner365 <[email protected]>"
110+
"florian-lackner365 <[email protected]>",
111+
"Alejandro Marulanda <[email protected]>"
111112
]
112113
}

src/core/runtime-field.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,20 @@ class RuntimeField {
4646
/**
4747
* Sets the source of the script.
4848
* @param {string} script
49-
* @returns {void}
49+
* @returns {RuntimeField} returns `this` so that calls can be chained.
5050
*/
5151
script(script) {
5252
this._body.script = {
5353
source: script
5454
};
5555
this._isScriptSet = true;
56+
return this;
5657
}
5758

5859
/**
5960
* Sets the type of the runtime field.
6061
* @param {string} type One of `boolean`, `composite`, `date`, `double`, `geo_point`, `ip`, `keyword`, `long`, `lookup`.
61-
* @returns {void}
62+
* @returns {RuntimeField} returns `this` so that calls can be chained.
6263
*/
6364
type(type) {
6465
const typeLower = type.toLowerCase();
@@ -67,6 +68,34 @@ class RuntimeField {
6768
}
6869
this._body.type = typeLower;
6970
this._isTypeSet = true;
71+
return this;
72+
}
73+
74+
/**
75+
* Specifies the language the script is written in. Defaults to `painless` but
76+
* may be set to any of languages listed in [Scripting](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html).
77+
*
78+
* @param {string} lang The language for the script.
79+
* @returns {RuntimeField} returns `this` so that calls can be chained.
80+
*/
81+
lang(lang) {
82+
if (!isNil(this._body.script)) {
83+
this._body.script.lang = lang;
84+
}
85+
return this;
86+
}
87+
88+
/**
89+
* Specifies any named parameters that are passed into the script as variables.
90+
*
91+
* @param {Object} params Named parameters to be passed to script.
92+
* @returns {RuntimeField} returns `this` so that calls can be chained.
93+
*/
94+
params(params) {
95+
if (!isNil(this._body.script)) {
96+
this._body.script.params = params;
97+
}
98+
return this;
7099
}
71100

72101
/**

src/index.d.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9009,17 +9009,34 @@ declare namespace esb {
90099009
* Sets the type of the runtime field.
90109010
*
90119011
* @param {string} type One of `boolean`, `composite`, `date`, `double`, `geo_point`, `ip`, `keyword`, `long`, `lookup`.
9012-
* @returns {void}
9012+
* @returns {RuntimeField} returns `this` so that calls can be chained.
90139013
*/
9014-
type(type: 'boolean' | 'composite' | 'date' | 'double' | 'geo_point' | 'ip' | 'keyword' | 'long' | 'lookup'): void;
9014+
type(type: 'boolean' | 'composite' | 'date' | 'double' | 'geo_point' | 'ip' | 'keyword' | 'long' | 'lookup'): this;
90159015

90169016
/**
90179017
* Sets the source of the script.
90189018
*
90199019
* @param {string} script
9020-
* @returns {void}
9020+
* @returns {RuntimeField} returns `this` so that calls can be chained.
90219021
*/
9022-
script(script: string): void;
9022+
script(script: string): this;
9023+
9024+
/**
9025+
* Specifies the language the script is written in. Defaults to `painless` but
9026+
* may be set to any of languages listed in [Scripting](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html).
9027+
*
9028+
* @param {string} lang The language for the script.
9029+
* @returns {RuntimeField} returns `this` so that calls can be chained.
9030+
*/
9031+
lang(lang: string): this;
9032+
9033+
/**
9034+
* Specifies any named parameters that are passed into the script as variables.
9035+
*
9036+
* @param {object} params Named parameters to be passed to script.
9037+
* @returns {RuntimeField} returns `this` so that calls can be chained.
9038+
*/
9039+
params(params: object): this;
90239040

90249041
/**
90259042
* Override default `toJSON` to return DSL representation for the `script`.

test/core-test/request-body-search.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ const runtimeFieldB = new RuntimeField(
4545
'boolean',
4646
"emit(doc['qty'].value > 10)"
4747
);
48+
const runtimeFieldC = new RuntimeField(
49+
'keyword',
50+
"emit(doc['my_field_name'].value * params.factor)"
51+
)
52+
.lang('painless')
53+
.params({ factor: 2.0 });
4854

4955
const scriptA = new Script('inline', "doc['my_field_name'].value * 2").lang(
5056
'painless'
@@ -178,6 +184,22 @@ test(setsOption, 'runtimeMappings', {
178184
},
179185
keyName: 'runtime_mappings'
180186
});
187+
test('Runtime mappging with lang and params', setsOption, 'runtimeMapping', {
188+
param: ['test1', runtimeFieldC],
189+
propValue: {
190+
test1: {
191+
type: 'keyword',
192+
script: {
193+
lang: 'painless',
194+
source: "emit(doc['my_field_name'].value * params.factor)",
195+
params: {
196+
factor: 2.0
197+
}
198+
}
199+
}
200+
},
201+
keyName: 'runtime_mappings'
202+
});
181203

182204
test(setsOption, 'scriptField', {
183205
param: ['test1', scriptA],

test/core-test/runtime-field.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,29 @@ test('script method sets script source', t => {
5151
};
5252
t.deepEqual(fieldA.toJSON(), expected);
5353
});
54+
55+
test('set script, lang and params', t => {
56+
const fieldA = new RuntimeField('keyword');
57+
fieldA.script("emit(doc['my_field_name'].value * params.factor)");
58+
fieldA.lang('painless');
59+
fieldA.params({ factor: 2.0 });
60+
const expected = {
61+
type: 'keyword',
62+
script: {
63+
lang: 'painless',
64+
source: "emit(doc['my_field_name'].value * params.factor)",
65+
params: {
66+
factor: 2.0
67+
}
68+
}
69+
};
70+
t.deepEqual(fieldA.toJSON(), expected);
71+
});
72+
73+
test("don't set lang and params if script is not set", t => {
74+
const fieldA = new RuntimeField('keyword');
75+
fieldA.lang('painless');
76+
fieldA.params({ factor: 2.0 });
77+
const error = t.throws(() => fieldA.toJSON());
78+
t.is(error.message, '`script` should be set');
79+
});

0 commit comments

Comments
 (0)