Skip to content

Commit

Permalink
feat: Runtime field script params (#202)
Browse files Browse the repository at this point in the history
Co-authored-by: Alejandro Marulanda <alejandro.marulanda@netcentric.biz>
  • Loading branch information
alejokf and Alejandro Marulanda authored Oct 30, 2024
1 parent 78de179 commit 7150119
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"kennylindahl <haxxblaster@gmail.com>",
"foxstarius <aj.franzon@gmail.com>",
"sandeep952 <sandy335582@gmail.com>",
"florian-lackner365 <florian.lackner@365talents.com>"
"florian-lackner365 <florian.lackner@365talents.com>",
"Alejandro Marulanda <alejokf@gmail.com>"
]
}
33 changes: 31 additions & 2 deletions src/core/runtime-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

/**
Expand Down
25 changes: 21 additions & 4 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
22 changes: 22 additions & 0 deletions test/core-test/request-body-search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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],
Expand Down
26 changes: 26 additions & 0 deletions test/core-test/runtime-field.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit 7150119

Please sign in to comment.