Skip to content

Commit

Permalink
allows passing multiple dataviews to buildEsQuery (#140162)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored Sep 14, 2022
1 parent 9fe5dd5 commit 59ea09f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/kbn-es-query/src/es_query/build_es_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function removeMatchAll<T>(filters: T[]) {
* @public
*/
export function buildEsQuery(
indexPattern: DataViewBase | undefined,
indexPattern: DataViewBase | DataViewBase[] | undefined,
queries: AnyQuery | AnyQuery[],
filters: Filter | Filter[],
config: EsQueryConfig = {
Expand All @@ -60,7 +60,7 @@ export function buildEsQuery(
const validQueries = queries.filter(isOfQueryType).filter((query) => has(query, 'query'));
const queriesByLanguage = groupBy(validQueries, 'language');
const kueryQuery = buildQueryFromKuery(
indexPattern,
Array.isArray(indexPattern) ? indexPattern[0] : indexPattern,
queriesByLanguage.kuery,
{ allowLeadingWildcards: config.allowLeadingWildcards },
{
Expand Down
39 changes: 39 additions & 0 deletions packages/kbn-es-query/src/es_query/from_filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('build query', () => {
const indexPattern: DataViewBase = {
fields,
title: 'dataView',
id: '1',
};

describe('buildQueryFromFilters', () => {
Expand Down Expand Up @@ -201,5 +202,43 @@ describe('build query', () => {
const result = buildQueryFromFilters(filters, indexPattern, { nestedIgnoreUnmapped: true });
expect(result.filter).toEqual(expectedESQueries);
});

test('should work with multiple data views', () => {
const indexPattern2: DataViewBase = {
fields,
title: 'dataView',
id: '2',
};

const filters = [
{
query: { query_string: { query: 'foo' } },
meta: { index: '1' },
},
{
query: { query_string: { query: 'bar' } },
meta: { index: '2' },
},
] as Filter[];

const result = buildQueryFromFilters(filters, [indexPattern, indexPattern2], {
ignoreFilterIfFieldNotInIndex: false,
});

expect(result.filter).toMatchInlineSnapshot(`
Array [
Object {
"query_string": Object {
"query": "foo",
},
},
Object {
"query_string": Object {
"query": "bar",
},
},
]
`);
});
});
});
27 changes: 17 additions & 10 deletions packages/kbn-es-query/src/es_query/from_filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,34 @@ export interface EsQueryFiltersConfig {
* @public
*/
export const buildQueryFromFilters = (
filters: Filter[] = [],
indexPattern: DataViewBase | undefined,
inputFilters: Filter[] = [],
inputDataViews: DataViewBase | DataViewBase[] | undefined,
{ ignoreFilterIfFieldNotInIndex = false, nestedIgnoreUnmapped }: EsQueryFiltersConfig = {
ignoreFilterIfFieldNotInIndex: false,
}
): BoolQuery => {
filters = filters.filter((filter) => filter && !isFilterDisabled(filter));
const filters = inputFilters.filter((filter) => filter && !isFilterDisabled(filter));
const indexPatterns = Array.isArray(inputDataViews) ? inputDataViews : [inputDataViews];

const findIndexPattern = (id: string | undefined) => {
return indexPatterns.find((index) => index?.id === id) || indexPatterns[0];
};

const filtersToESQueries = (negate: boolean) => {
return filters
.filter((f) => !!f)
.filter(filterNegate(negate))
.filter(
(filter) => !ignoreFilterIfFieldNotInIndex || filterMatchesIndex(filter, indexPattern)
)
.filter((filter) => {
const indexPattern = findIndexPattern(filter.meta?.index);
return !ignoreFilterIfFieldNotInIndex || filterMatchesIndex(filter, indexPattern);
})
.map((filter) => {
return migrateFilter(filter, indexPattern);
const indexPattern = findIndexPattern(filter.meta?.index);
const migratedFilter = migrateFilter(filter, indexPattern);
return handleNestedFilter(migratedFilter, indexPattern, {
ignoreUnmapped: nestedIgnoreUnmapped,
});
})
.map((filter) =>
handleNestedFilter(filter, indexPattern, { ignoreUnmapped: nestedIgnoreUnmapped })
)
.map(cleanFilter)
.map(translateToQuery);
};
Expand Down

0 comments on commit 59ea09f

Please sign in to comment.