From e578c5805f651c85ea260f3a5af36eac4529a5f4 Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Wed, 3 Mar 2021 11:04:13 +0200 Subject: [PATCH] [Bug] Fix filter creation for numeric scripted fields in Discover (#93224) * fixes #74301 * comment * tests * test title --- .../es_query/filters/phrase_filter.test.ts | 54 ++++++++++++++++++- .../common/es_query/filters/phrase_filter.ts | 15 ++++-- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/plugins/data/common/es_query/filters/phrase_filter.test.ts b/src/plugins/data/common/es_query/filters/phrase_filter.test.ts index 1505f6a628ddc..513f0e29b5b24 100644 --- a/src/plugins/data/common/es_query/filters/phrase_filter.test.ts +++ b/src/plugins/data/common/es_query/filters/phrase_filter.test.ts @@ -27,10 +27,25 @@ describe('Phrase filter builder', () => { expect(typeof buildPhraseFilter).toBe('function'); }); - it('should return a match query filter when passed a standard field', () => { + it('should return a match query filter when passed a standard string field', () => { + const field = getField('extension'); + + expect(buildPhraseFilter(field, 'jpg', indexPattern)).toEqual({ + meta: { + index: 'id', + }, + query: { + match_phrase: { + extension: 'jpg', + }, + }, + }); + }); + + it('should return a match query filter when passed a standard numeric field', () => { const field = getField('bytes'); - expect(buildPhraseFilter(field, 5, indexPattern)).toEqual({ + expect(buildPhraseFilter(field, '5', indexPattern)).toEqual({ meta: { index: 'id', }, @@ -42,6 +57,21 @@ describe('Phrase filter builder', () => { }); }); + it('should return a match query filter when passed a standard bool field', () => { + const field = getField('ssl'); + + expect(buildPhraseFilter(field, 'true', indexPattern)).toEqual({ + meta: { + index: 'id', + }, + query: { + match_phrase: { + ssl: true, + }, + }, + }); + }); + it('should return a script filter when passed a scripted field', () => { const field = getField('script number'); @@ -61,6 +91,26 @@ describe('Phrase filter builder', () => { }, }); }); + + it('should return a script filter when passed a scripted field with numeric conversion', () => { + const field = getField('script number'); + + expect(buildPhraseFilter(field, '5', indexPattern)).toEqual({ + meta: { + index: 'id', + field: 'script number', + }, + script: { + script: { + lang: 'expression', + params: { + value: 5, + }, + source: '(1234) == value', + }, + }, + }); + }); }); describe('buildInlineScriptForPhraseFilter', () => { diff --git a/src/plugins/data/common/es_query/filters/phrase_filter.ts b/src/plugins/data/common/es_query/filters/phrase_filter.ts index 2aeaa5272787f..364e8dc1b035f 100644 --- a/src/plugins/data/common/es_query/filters/phrase_filter.ts +++ b/src/plugins/data/common/es_query/filters/phrase_filter.ts @@ -96,9 +96,14 @@ export const getPhraseScript = (field: IFieldType, value: string) => { }; }; -// See https://github.com/elastic/elasticsearch/issues/20941 and https://github.com/elastic/kibana/issues/8677 -// and https://github.com/elastic/elasticsearch/pull/22201 -// for the reason behind this change. Aggs now return boolean buckets with a key of 1 or 0. +/** + * See issues bellow for the reason behind this change. + * Values need to be converted to correct types for boolean \ numeric fields. + * https://github.com/elastic/kibana/issues/74301 + * https://github.com/elastic/kibana/issues/8677 + * https://github.com/elastic/elasticsearch/issues/20941 + * https://github.com/elastic/elasticsearch/pull/22201 + **/ export const getConvertedValueForField = (field: IFieldType, value: any) => { if (typeof value !== 'boolean' && field.type === 'boolean') { if ([1, 'true'].includes(value)) { @@ -109,6 +114,10 @@ export const getConvertedValueForField = (field: IFieldType, value: any) => { throw new Error(`${value} is not a valid boolean value for boolean field ${field.name}`); } } + + if (typeof value !== 'number' && field.type === 'number') { + return Number(value); + } return value; };