diff --git a/src/plugins/data/common/es_query/filters/phrases_filter.ts b/src/plugins/data/common/es_query/filters/phrases_filter.ts index 8a79472154493..2694461fc1930 100644 --- a/src/plugins/data/common/es_query/filters/phrases_filter.ts +++ b/src/plugins/data/common/es_query/filters/phrases_filter.ts @@ -41,11 +41,6 @@ export const buildPhrasesFilter = ( const type = FILTERS.PHRASES; const key = field.name; - const format = (f: IFieldType, value: any) => - f && f.format && f.format.convert ? f.format.convert(value) : value; - - const value = params.map((v: any) => format(field, v)).join(', '); - let should; if (field.scripted) { should = params.map((v: any) => ({ @@ -60,7 +55,7 @@ export const buildPhrasesFilter = ( } return { - meta: { index, type, key, value, params }, + meta: { index, type, key, params }, query: { bool: { should, diff --git a/src/plugins/data/common/es_query/filters/range_filter.ts b/src/plugins/data/common/es_query/filters/range_filter.ts index 7bc7a8cff7487..9f1d9a5d08926 100644 --- a/src/plugins/data/common/es_query/filters/range_filter.ts +++ b/src/plugins/data/common/es_query/filters/range_filter.ts @@ -84,10 +84,7 @@ export const getRangeFilterField = (filter: RangeFilter) => { }; const formatValue = (field: IFieldType, params: any[]) => - map(params, (val: any, key: string) => get(operators, key) + format(field, val)).join(' '); - -const format = (field: IFieldType, value: any) => - field && field.format && field.format.convert ? field.format.convert(value) : value; + map(params, (val: any, key: string) => get(operators, key) + val).join(' '); // Creates a filter where the value for the given field is in the given range // params should be an object containing `lt`, `lte`, `gt`, and/or `gte` diff --git a/src/plugins/data/common/search/expressions/phrase_filter.test.ts b/src/plugins/data/common/search/expressions/phrase_filter.test.ts index 39bd907513a0d..a61cc0bfd68ab 100644 --- a/src/plugins/data/common/search/expressions/phrase_filter.test.ts +++ b/src/plugins/data/common/search/expressions/phrase_filter.test.ts @@ -32,7 +32,6 @@ describe('interpreter/functions#phraseFilter', () => { "something", ], "type": "phrases", - "value": "test, something", }, "query": Object { "bool": Object { diff --git a/src/plugins/data/public/query/filter_manager/lib/get_display_value.test.ts b/src/plugins/data/public/query/filter_manager/lib/get_display_value.test.ts new file mode 100644 index 0000000000000..48e1007534769 --- /dev/null +++ b/src/plugins/data/public/query/filter_manager/lib/get_display_value.test.ts @@ -0,0 +1,45 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { stubIndexPattern, phraseFilter } from 'src/plugins/data/common/stubs'; +import { getDisplayValueFromFilter } from './get_display_value'; + +describe('getDisplayValueFromFilter', () => { + it('returns the value if string', () => { + phraseFilter.meta.value = 'abc'; + const displayValue = getDisplayValueFromFilter(phraseFilter, [stubIndexPattern]); + expect(displayValue).toBe('abc'); + }); + + it('returns the value if undefined', () => { + phraseFilter.meta.value = undefined; + const displayValue = getDisplayValueFromFilter(phraseFilter, [stubIndexPattern]); + expect(displayValue).toBe(''); + }); + + it('calls the value function if proivided', () => { + // The type of value currently doesn't match how it's used. Refactor needed. + phraseFilter.meta.value = jest.fn((x) => { + return 'abc'; + }) as any; + const displayValue = getDisplayValueFromFilter(phraseFilter, [stubIndexPattern]); + expect(displayValue).toBe('abc'); + expect(phraseFilter.meta.value).toHaveBeenCalledWith(undefined); + }); + + it('calls the value function if proivided, with formatter', () => { + stubIndexPattern.getFormatterForField = jest.fn().mockReturnValue('banana'); + phraseFilter.meta.value = jest.fn((x) => { + return x + 'abc'; + }) as any; + const displayValue = getDisplayValueFromFilter(phraseFilter, [stubIndexPattern]); + expect(stubIndexPattern.getFormatterForField).toHaveBeenCalledTimes(1); + expect(phraseFilter.meta.value).toHaveBeenCalledWith('banana'); + expect(displayValue).toBe('bananaabc'); + }); +}); diff --git a/src/plugins/data/public/query/filter_manager/lib/get_display_value.ts b/src/plugins/data/public/query/filter_manager/lib/get_display_value.ts index 45c6167f600bc..1ccfaacb24e4b 100644 --- a/src/plugins/data/public/query/filter_manager/lib/get_display_value.ts +++ b/src/plugins/data/public/query/filter_manager/lib/get_display_value.ts @@ -28,11 +28,14 @@ function getValueFormatter(indexPattern?: IIndexPattern, key?: string) { } export function getDisplayValueFromFilter(filter: Filter, indexPatterns: IIndexPattern[]): string { - if (typeof filter.meta.value === 'function') { + const { key, value } = filter.meta; + if (typeof value === 'function') { const indexPattern = getIndexPatternFromFilter(filter, indexPatterns); - const valueFormatter: any = getValueFormatter(indexPattern, filter.meta.key); - return (filter.meta.value as any)(valueFormatter); + const valueFormatter = getValueFormatter(indexPattern, key); + // TODO: distinguish between FilterMeta which is serializable to mapped FilterMeta + // Where value can be a function. + return (value as any)(valueFormatter); } else { - return filter.meta.value || ''; + return value || ''; } } diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrases.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrases.ts index bfd528264b00f..5601dd66e5206 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrases.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrases.ts @@ -6,14 +6,29 @@ * Side Public License, v 1. */ -import { Filter, isPhrasesFilter } from '../../../../../common'; +import { Filter, FilterValueFormatter, isPhrasesFilter } from '../../../../../common'; + +const getFormattedValueFn = (params: any) => { + return (formatter?: FilterValueFormatter) => { + return params + .map((v: any) => { + return formatter ? formatter.convert(v) : v; + }) + .join(', '); + }; +}; export const mapPhrases = (filter: Filter) => { if (!isPhrasesFilter(filter)) { throw filter; } - const { type, key, value, params } = filter.meta; + const { type, key, params } = filter.meta; - return { type, key, value, params }; + return { + type, + key, + value: getFormattedValueFn(params), + params, + }; };