Skip to content

Commit

Permalink
[Discover][Dashboard] Fix creating filters for non-distinct data (#99400
Browse files Browse the repository at this point in the history
)

* Fix creating filters for non-distinct values in the embedded search (#92876)
* Fix creating non-unique filters in the visualization (#66595)
  • Loading branch information
dokmic committed May 12, 2021
1 parent 5106f42 commit 9e588a6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,11 @@ describe('createFiltersFromValueClick', () => {
expect(rangeFilter.range.bytes.lt).toEqual(2078);
}
});

test('handles non-unique filters', async () => {
const [point] = dataPoints;
const filters = await createFiltersFromValueClickAction({ data: [point, point] });

expect(filters.length).toEqual(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import _ from 'lodash';
import { Datatable } from '../../../../../plugins/expressions/public';
import { esFilters, Filter } from '../../../public';
import { getIndexPatterns, getSearchService } from '../../../public/services';
Expand Down Expand Up @@ -140,5 +141,7 @@ export const createFiltersFromValueClickAction = async ({
})
);

return esFilters.mapAndFlattenFilters(filters);
return _.uniqWith(esFilters.mapAndFlattenFilters(filters), (a, b) =>
esFilters.compareFilters(a, b, esFilters.COMPARE_ALL_OPTIONS)
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,22 @@ describe('Generate filters', () => {
[FIELD.name]: ANOTHER_PHRASE,
});
});

it('should use only distinct values', () => {
const ANOTHER_PHRASE = 'another-value';
const filters = generateFilters(
mockFilterManager,
FIELD,
[PHRASE_VALUE, ANOTHER_PHRASE, PHRASE_VALUE, ANOTHER_PHRASE],
'',
INDEX_NAME
);
expect(filters).toHaveLength(2);
expect((filters[0] as PhraseFilter).query.match_phrase).toEqual({
[FIELD.name]: PHRASE_VALUE,
});
expect((filters[1] as PhraseFilter).query.match_phrase).toEqual({
[FIELD.name]: ANOTHER_PHRASE,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function generateFilters(
operation: string,
index: string
): Filter[] {
values = Array.isArray(values) ? values : [values];
values = Array.isArray(values) ? _.uniq(values) : [values];
const fieldObj = (_.isObject(field)
? field
: {
Expand Down

0 comments on commit 9e588a6

Please sign in to comment.