support query filter (#123988)

This commit is contained in:
Melissa Alvarez 2022-01-28 13:05:25 -07:00 committed by GitHub
parent fa38d1ae0d
commit a927e82b34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View file

@ -108,7 +108,7 @@ export class AnomalySource implements IVectorSource {
destroy(): void {}
getApplyGlobalQuery(): boolean {
return false;
return true;
}
getApplyForceRefresh(): boolean {
@ -323,8 +323,7 @@ export class AnomalySource implements IVectorSource {
}
isQueryAware(): boolean {
// IGNORE: This is only relevant if your source is backed by an index-pattern
return false;
return true;
}
isRefreshTimerAware(): boolean {

View file

@ -7,11 +7,14 @@
import { FeatureCollection, Feature, Geometry } from 'geojson';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { fromKueryExpression, luceneStringToDsl, toElasticsearchQuery } from '@kbn/es-query';
import { ESSearchResponse } from '../../../../../src/core/types/elasticsearch';
import { formatHumanReadableDateTimeSeconds } from '../../common/util/date_utils';
import type { MlApiServices } from '../application/services/ml_api_service';
import { MLAnomalyDoc } from '../../common/types/anomalies';
import { VectorSourceRequestMeta } from '../../../maps/common';
import { SEARCH_QUERY_LANGUAGE } from '../../common/constants/search';
import { getIndexPattern } from '../application/explorer/reducers/explorer_reducer/get_index_pattern';
export const ML_ANOMALY_LAYERS = {
TYPICAL: 'typical',
@ -37,19 +40,38 @@ export async function getResultsForJobId(
locationType: MlAnomalyLayersType,
searchFilters: VectorSourceRequestMeta
): Promise<FeatureCollection> {
const { timeFilters } = searchFilters;
const { query, timeFilters } = searchFilters;
const hasQuery = query && query.query !== '';
let queryFilter;
// @ts-ignore missing properties from ExplorerJob - those fields aren't required for this
const indexPattern = getIndexPattern([{ id: jobId }]);
if (hasQuery && query.language === SEARCH_QUERY_LANGUAGE.KUERY) {
queryFilter = toElasticsearchQuery(fromKueryExpression(query.query), indexPattern);
} else if (hasQuery && query?.language === SEARCH_QUERY_LANGUAGE.LUCENE) {
queryFilter = luceneStringToDsl(query.query);
}
const must: estypes.QueryDslQueryContainer[] = [
{ term: { job_id: jobId } },
{ term: { result_type: 'record' } },
];
let bool: estypes.QueryDslBoolQuery = {
must,
};
if (queryFilter && queryFilter.bool) {
bool = { ...bool, ...queryFilter.bool };
} else if (queryFilter) {
// @ts-ignore push doesn't exist on type QueryDslQueryContainer | QueryDslQueryContainer[] | undefined
bool.must.push(queryFilter);
}
// Query to look for the highest scoring anomaly.
const body: estypes.SearchRequest['body'] = {
query: {
bool: {
must,
},
bool,
},
size: 1000,
_source: {