Skip to content

Commit 4064b02

Browse files
authored
[Discover][Dashboard] Fix creating filters for non-distinct data (#99400)
* Fix creating filters for non-distinct values in the embedded search (#92876) * Fix creating non-unique filters in the visualization (#66595)
1 parent 9907903 commit 4064b02

4 files changed

Lines changed: 30 additions & 2 deletions

File tree

src/plugins/data/public/actions/filters/create_filters_from_value_click.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,11 @@ describe('createFiltersFromValueClick', () => {
105105
expect(rangeFilter.range.bytes.lt).toEqual(2078);
106106
}
107107
});
108+
109+
test('handles non-unique filters', async () => {
110+
const [point] = dataPoints;
111+
const filters = await createFiltersFromValueClickAction({ data: [point, point] });
112+
113+
expect(filters.length).toEqual(1);
114+
});
108115
});

src/plugins/data/public/actions/filters/create_filters_from_value_click.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9+
import _ from 'lodash';
910
import { Datatable } from '../../../../../plugins/expressions/public';
1011
import { esFilters, Filter } from '../../../public';
1112
import { getIndexPatterns, getSearchService } from '../../../public/services';
@@ -140,5 +141,7 @@ export const createFiltersFromValueClickAction = async ({
140141
})
141142
);
142143

143-
return esFilters.mapAndFlattenFilters(filters);
144+
return _.uniqWith(esFilters.mapAndFlattenFilters(filters), (a, b) =>
145+
esFilters.compareFilters(a, b, esFilters.COMPARE_ALL_OPTIONS)
146+
);
144147
};

src/plugins/data/public/query/filter_manager/lib/generate_filter.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,22 @@ describe('Generate filters', () => {
174174
[FIELD.name]: ANOTHER_PHRASE,
175175
});
176176
});
177+
178+
it('should use only distinct values', () => {
179+
const ANOTHER_PHRASE = 'another-value';
180+
const filters = generateFilters(
181+
mockFilterManager,
182+
FIELD,
183+
[PHRASE_VALUE, ANOTHER_PHRASE, PHRASE_VALUE, ANOTHER_PHRASE],
184+
'',
185+
INDEX_NAME
186+
);
187+
expect(filters).toHaveLength(2);
188+
expect((filters[0] as PhraseFilter).query.match_phrase).toEqual({
189+
[FIELD.name]: PHRASE_VALUE,
190+
});
191+
expect((filters[1] as PhraseFilter).query.match_phrase).toEqual({
192+
[FIELD.name]: ANOTHER_PHRASE,
193+
});
194+
});
177195
});

src/plugins/data/public/query/filter_manager/lib/generate_filters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function generateFilters(
7171
operation: string,
7272
index: string
7373
): Filter[] {
74-
values = Array.isArray(values) ? values : [values];
74+
values = Array.isArray(values) ? _.uniq(values) : [values];
7575
const fieldObj = (_.isObject(field)
7676
? field
7777
: {

0 commit comments

Comments
 (0)