[ML] Data Frame Analytics creation wizard: add support for filters in saved searches (#130744)

* support savedSearch filters in DFA results

* fix linting error

* fix error when no search has filter but no query
This commit is contained in:
Melissa Alvarez 2022-04-26 15:15:32 -06:00 committed by GitHub
parent eedfec19cc
commit d371f3aaee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,12 +7,12 @@
import { useState, useEffect } from 'react';
import {
buildEsQuery,
buildQueryFromFilters,
decorateQuery,
fromKueryExpression,
luceneStringToDsl,
toElasticsearchQuery,
} from '@kbn/es-query';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { useMlContext } from '../../../../../contexts/ml';
import { SEARCH_QUERY_LANGUAGE } from '../../../../../../../common/constants/search';
import { getQueryFromSavedSearchObject } from '../../../../../util/index_utils';
@ -36,19 +36,42 @@ export function useSavedSearch() {
const { currentSavedSearch, currentDataView, kibanaConfig } = mlContext;
const getQueryData = () => {
let qry: estypes.QueryDslQueryContainer = {};
let qry: any = {};
let qryString;
if (currentSavedSearch !== null) {
const { query } = getQueryFromSavedSearchObject(currentSavedSearch);
const { query, filter } = getQueryFromSavedSearchObject(currentSavedSearch);
const queryLanguage = query.language;
qryString = query.query;
if (queryLanguage === SEARCH_QUERY_LANGUAGE.KUERY) {
const ast = fromKueryExpression(qryString);
qry = toElasticsearchQuery(ast, currentDataView);
const filterQuery = buildQueryFromFilters(filter, currentDataView);
if (qry.bool === undefined) {
qry.bool = {};
// toElasticsearchQuery may add a single match_all item to the
// root of its returned query, rather than putting it inside
// a bool.should
// in this case, move it to a bool.should
if (qry.match_all !== undefined) {
qry.bool.should = {
match_all: qry.match_all,
};
delete qry.match_all;
}
}
if (Array.isArray(qry.bool.filter) === false) {
qry.bool.filter = qry.bool.filter === undefined ? [] : [qry.bool.filter];
}
if (Array.isArray(qry.bool.must_not) === false) {
qry.bool.must_not = qry.bool.must_not === undefined ? [] : [qry.bool.must_not];
}
qry.bool.filter = [...qry.bool.filter, ...filterQuery.filter];
qry.bool.must_not = [...qry.bool.must_not, ...filterQuery.must_not];
} else {
qry = luceneStringToDsl(qryString);
qry = buildEsQuery(currentDataView, [query], filter);
decorateQuery(qry, kibanaConfig.get('query:queryString:options'));
}