From 7f2dbe51a0e470c48c6ab80a8192e40c9da54b9e Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Wed, 24 Nov 2021 07:37:46 -0700 Subject: [PATCH] [Maps] fix creating filter from array fields (#119548) * [Maps] fix creating filter from array fields * eslint * remove unneeded cast --- .../tooltips/es_tooltip_property.test.ts | 34 +++++++++++++++++++ .../classes/tooltips/es_tooltip_property.ts | 9 +++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/maps/public/classes/tooltips/es_tooltip_property.test.ts b/x-pack/plugins/maps/public/classes/tooltips/es_tooltip_property.test.ts index 8ee9d30ecbf89..e2d93a6937675 100644 --- a/x-pack/plugins/maps/public/classes/tooltips/es_tooltip_property.test.ts +++ b/x-pack/plugins/maps/public/classes/tooltips/es_tooltip_property.test.ts @@ -108,6 +108,40 @@ describe('getESFilters', () => { ]); }); + test('Should return phrase filters when field value is an array', async () => { + const esTooltipProperty = new ESTooltipProperty( + new TooltipProperty(featurePropertyField.getName(), await featurePropertyField.getLabel(), [ + 'my value', + 'my other value', + ]), + indexPattern, + featurePropertyField, + APPLY_GLOBAL_QUERY + ); + expect(await esTooltipProperty.getESFilters()).toEqual([ + { + meta: { + index: 'indexPatternId', + }, + query: { + match_phrase: { + ['machine.os']: 'my value', + }, + }, + }, + { + meta: { + index: 'indexPatternId', + }, + query: { + match_phrase: { + ['machine.os']: 'my other value', + }, + }, + }, + ]); + }); + test('Should return NOT exists filter for null values', async () => { const esTooltipProperty = new ESTooltipProperty( new TooltipProperty( diff --git a/x-pack/plugins/maps/public/classes/tooltips/es_tooltip_property.ts b/x-pack/plugins/maps/public/classes/tooltips/es_tooltip_property.ts index 8b08d3a195a34..1b677b196c997 100644 --- a/x-pack/plugins/maps/public/classes/tooltips/es_tooltip_property.ts +++ b/x-pack/plugins/maps/public/classes/tooltips/es_tooltip_property.ts @@ -96,13 +96,16 @@ export class ESTooltipProperty implements ITooltipProperty { return []; } - const value = this.getRawValue(); - if (value == null) { + const rawValue = this.getRawValue(); + if (rawValue == null) { const existsFilter = esFilters.buildExistsFilter(indexPatternField, this._indexPattern); existsFilter.meta.negate = true; return [existsFilter]; } else { - return [esFilters.buildPhraseFilter(indexPatternField, value as string, this._indexPattern)]; + const values = Array.isArray(rawValue) ? (rawValue as string[]) : [rawValue as string]; + return values.map((value) => { + return esFilters.buildPhraseFilter(indexPatternField, value, this._indexPattern); + }); } } }