Skip to content

Commit

Permalink
Fix bug with scripted number range filters (elastic#99554) (elastic#9…
Browse files Browse the repository at this point in the history
…9816)

Co-authored-by: Lukas Olson <[email protected]>
  • Loading branch information
kibanamachine and lukasolson authored May 11, 2021
1 parent 6f77e64 commit aebb116
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/plugins/data/common/es_query/filters/range_filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ describe('Range filter builder', () => {
});
});

it('should convert strings to numbers if the field is scripted and type number', () => {
const field = getField('script number');

expect(buildRangeFilter(field, { gte: '1', lte: '3' }, indexPattern)).toEqual({
meta: {
field: 'script number',
index: 'id',
params: {},
},
script: {
script: {
lang: 'expression',
source: '(' + field!.script + ')>=gte && (' + field!.script + ')<=lte',
params: {
value: '>=1 <=3',
gte: 1,
lte: 3,
},
},
},
});
});

it('should wrap painless scripts in comparator lambdas', () => {
const field = getField('script date');
const expected =
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/data/common/es_query/filters/range_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ export const buildRangeFilter = (
};

export const getRangeScript = (field: IFieldType, params: RangeFilterParams) => {
const knownParams = pickBy(params, (val, key: any) => key in operators);
const knownParams = mapValues(
pickBy(params, (val, key: any) => key in operators),
(value) => (field.type === 'number' && typeof value === 'string' ? parseFloat(value) : value)
);
let script = map(
knownParams,
(val: any, key: string) => '(' + field.script + ')' + get(operators, key) + key
Expand Down

0 comments on commit aebb116

Please sign in to comment.