[Bug] Fix filter creation for numeric scripted fields in Discover (#93224)

* fixes #74301

* comment

* tests

* test title
This commit is contained in:
Liza Katz 2021-03-03 11:04:13 +02:00 committed by GitHub
parent 80a34a3e1d
commit 6ee568c997
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 5 deletions

View file

@ -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', () => {

View file

@ -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;
};