Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime field script params #202

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>",
"foxstarius <[email protected]>",
"sandeep952 <[email protected]>",
"florian-lackner365 <[email protected]>"
"florian-lackner365 <[email protected]>",
"Alejandro Marulanda <[email protected]>"
]
}
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');
});