[Bug] Fix filter creation for numeric scripted fields in Discover (#93224)
* fixes #74301 * comment * tests * test title
This commit is contained in:
parent
80a34a3e1d
commit
6ee568c997
2 changed files with 64 additions and 5 deletions
|
@ -27,10 +27,25 @@ describe('Phrase filter builder', () => {
|
||||||
expect(typeof buildPhraseFilter).toBe('function');
|
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');
|
const field = getField('bytes');
|
||||||
|
|
||||||
expect(buildPhraseFilter(field, 5, indexPattern)).toEqual({
|
expect(buildPhraseFilter(field, '5', indexPattern)).toEqual({
|
||||||
meta: {
|
meta: {
|
||||||
index: 'id',
|
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', () => {
|
it('should return a script filter when passed a scripted field', () => {
|
||||||
const field = getField('script number');
|
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', () => {
|
describe('buildInlineScriptForPhraseFilter', () => {
|
||||||
|
|
|
@ -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
|
* See issues bellow for the reason behind this change.
|
||||||
// for the reason behind this change. Aggs now return boolean buckets with a key of 1 or 0.
|
* 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) => {
|
export const getConvertedValueForField = (field: IFieldType, value: any) => {
|
||||||
if (typeof value !== 'boolean' && field.type === 'boolean') {
|
if (typeof value !== 'boolean' && field.type === 'boolean') {
|
||||||
if ([1, 'true'].includes(value)) {
|
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}`);
|
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;
|
return value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue