diff --git a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/__mocks__/params_match_all.ts b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/__mocks__/params_match_all.ts new file mode 100644 index 00000000000000..a81ba523caa43c --- /dev/null +++ b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/__mocks__/params_match_all.ts @@ -0,0 +1,19 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export const paramsMock = { + index: 'the-index', + timeFieldName: 'the-time-field-name', + start: 0, + end: 50, + baselineMin: 10, + baselineMax: 20, + deviationMin: 30, + deviationMax: 40, + includeFrozen: false, + searchQuery: '{ "match_all": {} }', +}; diff --git a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/__mocks__/params_search_query.ts b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/__mocks__/params_search_query.ts new file mode 100644 index 00000000000000..dd0ac80585403c --- /dev/null +++ b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/__mocks__/params_search_query.ts @@ -0,0 +1,16 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import type { AiopsLogRateAnalysisSchema } from '../../../../../common/api/log_rate_analysis/schema'; + +import { paramsMock } from './params_match_all'; +import { searchQueryMock } from './search_query'; + +export const paramsSearchQueryMock: AiopsLogRateAnalysisSchema = { + ...paramsMock, + searchQuery: searchQueryMock, +}; diff --git a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/__mocks__/search_query.ts b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/__mocks__/search_query.ts new file mode 100644 index 00000000000000..58be3bf3727115 --- /dev/null +++ b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/__mocks__/search_query.ts @@ -0,0 +1,16 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +// This is the format that gets passed on from the Kibana search bar. +export const searchQueryMock = JSON.stringify({ + bool: { + filter: [], + minimum_should_match: 1, + must_not: [], + should: [{ term: { 'the-term': { value: 'the-value' } } }], + }, +}); diff --git a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_categories.test.ts b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_categories.test.ts index 6c4825767c43fb..d38dd57c0afd2a 100644 --- a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_categories.test.ts +++ b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_categories.test.ts @@ -7,24 +7,12 @@ import { createRandomSamplerWrapper } from '@kbn/ml-random-sampler-utils'; +import { paramsMock } from './__mocks__/params_match_all'; import { getBaselineOrDeviationFilter, getCategoryRequest } from './fetch_categories'; describe('getBaselineOrDeviationFilter', () => { it('returns a filter that matches both baseline and deviation time range', () => { - const params = { - index: 'the-index', - timeFieldName: 'the-time-field-name', - start: 0, - end: 50, - baselineMin: 10, - baselineMax: 20, - deviationMin: 30, - deviationMax: 40, - includeFrozen: false, - searchQuery: '{ "match_all": {} }', - }; - - const baselineOrDeviationFilter = getBaselineOrDeviationFilter(params); + const baselineOrDeviationFilter = getBaselineOrDeviationFilter(paramsMock); expect(baselineOrDeviationFilter).toEqual({ bool: { @@ -47,25 +35,12 @@ describe('getBaselineOrDeviationFilter', () => { describe('getCategoryRequest', () => { it('returns the category request', () => { - const params = { - index: 'the-index', - timeFieldName: 'the-time-field-name', - start: 0, - end: 50, - baselineMin: 10, - baselineMax: 20, - deviationMin: 30, - deviationMax: 40, - includeFrozen: false, - searchQuery: '{ "match_all": {} }', - }; - const randomSamplerWrapper = createRandomSamplerWrapper({ probability: 0.1, seed: 1234, }); - const query = getCategoryRequest(params, 'the-field-name', randomSamplerWrapper); + const query = getCategoryRequest(paramsMock, 'the-field-name', randomSamplerWrapper); // Because the time range filter is covered by the should clauses that cover both // baseline (10,20) and deviation (30,40), we expect that there is no other diff --git a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_category_counts.test.ts b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_category_counts.test.ts index be9488164f35e8..b3fa6ff5f31a85 100644 --- a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_category_counts.test.ts +++ b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_category_counts.test.ts @@ -5,20 +5,9 @@ * 2.0. */ -import { getCategoryCountRequest, getCategoryCountMSearchRequest } from './fetch_category_counts'; +import { paramsMock } from './__mocks__/params_match_all'; -const params = { - index: 'the-index', - timeFieldName: 'the-time-field-name', - start: 0, - end: 50, - baselineMin: 10, - baselineMax: 20, - deviationMin: 30, - deviationMax: 40, - includeFrozen: false, - searchQuery: '{ "match_all": {} }', -}; +import { getCategoryCountRequest, getCategoryCountMSearchRequest } from './fetch_category_counts'; describe('getCategoryCountRequest', () => { it('returns the category count request', () => { @@ -29,11 +18,11 @@ describe('getCategoryCountRequest', () => { }; const query = getCategoryCountRequest( - params, + paramsMock, 'the-field-name', category, - params.baselineMin, - params.baselineMax + paramsMock.baselineMin, + paramsMock.baselineMax ); expect(query).toEqual({ @@ -81,11 +70,11 @@ describe('getCategoryCountMSearchRequest', () => { ]; const query = getCategoryCountMSearchRequest( - params, + paramsMock, 'the-field-name', categories, - params.baselineMin, - params.baselineMax + paramsMock.baselineMin, + paramsMock.baselineMax ); expect(query).toEqual([ diff --git a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_index_info.test.ts b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_index_info.test.ts index db14c30caebdcc..0344005c869f81 100644 --- a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_index_info.test.ts +++ b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/fetch_index_info.test.ts @@ -9,27 +9,14 @@ import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import type { ElasticsearchClient } from '@kbn/core/server'; -import type { AiopsLogRateAnalysisSchema } from '../../../../common/api/log_rate_analysis/schema'; +import { paramsSearchQueryMock } from './__mocks__/params_search_query'; import { fetchIndexInfo, getRandomDocsRequest } from './fetch_index_info'; -const params: AiopsLogRateAnalysisSchema = { - index: 'the-index', - timeFieldName: 'the-time-field-name', - start: 1577836800000, - end: 1609459200000, - baselineMin: 10, - baselineMax: 20, - deviationMin: 30, - deviationMax: 40, - includeFrozen: false, - searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}', -}; - describe('fetch_index_info', () => { describe('getRandomDocsRequest', () => { it('returns the most basic request body for a sample of random documents', () => { - const req = getRandomDocsRequest(params); + const req = getRandomDocsRequest(paramsSearchQueryMock); expect(req).toEqual({ body: { @@ -40,13 +27,20 @@ describe('fetch_index_info', () => { query: { bool: { filter: [ - { bool: { filter: [], must: [{ match_all: {} }], must_not: [] } }, + { + bool: { + filter: [], + minimum_should_match: 1, + must_not: [], + should: [{ term: { 'the-term': { value: 'the-value' } } }], + }, + }, { range: { 'the-time-field-name': { format: 'epoch_millis', - gte: 1577836800000, - lte: 1609459200000, + gte: 0, + lte: 50, }, }, }, @@ -59,7 +53,7 @@ describe('fetch_index_info', () => { size: 1000, track_total_hits: true, }, - index: params.index, + index: paramsSearchQueryMock.index, ignore_throttled: undefined, ignore_unavailable: true, }); @@ -105,7 +99,10 @@ describe('fetch_index_info', () => { search: esClientSearchMock, } as unknown as ElasticsearchClient; - const { totalDocCount, fieldCandidates } = await fetchIndexInfo(esClientMock, params); + const { totalDocCount, fieldCandidates } = await fetchIndexInfo( + esClientMock, + paramsSearchQueryMock + ); expect(fieldCandidates).toEqual(['myIpFieldName', 'myKeywordFieldName']); expect(totalDocCount).toEqual(5000000); diff --git a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_filters.test.ts b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_filters.test.ts index c34d81fa91bdff..22107bc5942d0e 100644 --- a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_filters.test.ts +++ b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_filters.test.ts @@ -5,43 +5,28 @@ * 2.0. */ +import { paramsSearchQueryMock } from './__mocks__/params_search_query'; + import { getFilters } from './get_filters'; describe('getFilters', () => { it('returns an empty array with no timeFieldName and searchQuery supplied', () => { const filters = getFilters({ - index: 'the-index', + ...paramsSearchQueryMock, timeFieldName: '', - searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}', - start: 1577836800000, - end: 1609459200000, - baselineMin: 10, - baselineMax: 20, - deviationMin: 30, - deviationMax: 40, }); expect(filters).toEqual([]); }); it('returns a range filter when timeFieldName is supplied', () => { - const filters = getFilters({ - index: 'the-index', - timeFieldName: 'the-time-field-name', - searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}', - start: 1577836800000, - end: 1609459200000, - baselineMin: 10, - baselineMax: 20, - deviationMin: 30, - deviationMax: 40, - }); + const filters = getFilters(paramsSearchQueryMock); expect(filters).toEqual([ { range: { 'the-time-field-name': { format: 'epoch_millis', - gte: 1577836800000, - lte: 1609459200000, + gte: 0, + lte: 50, }, }, }, diff --git a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_histogram_query.test.ts b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_histogram_query.test.ts index 95113d39f41c8c..39af620dd266ae 100644 --- a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_histogram_query.test.ts +++ b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_histogram_query.test.ts @@ -5,34 +5,30 @@ * 2.0. */ -import { getHistogramQuery } from './get_histogram_query'; +import { paramsSearchQueryMock } from './__mocks__/params_search_query'; -const paramsMock = { - index: 'the-index', - timeFieldName: 'the-time-field-name', - start: 1577836800000, - end: 1609459200000, - baselineMin: 10, - baselineMax: 20, - deviationMin: 30, - deviationMax: 40, - includeFrozen: false, - searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}', -}; +import { getHistogramQuery } from './get_histogram_query'; describe('getHistogramQuery', () => { it('returns histogram query without additional filters', () => { - const query = getHistogramQuery(paramsMock); + const query = getHistogramQuery(paramsSearchQueryMock); expect(query).toEqual({ bool: { filter: [ - { bool: { filter: [], must: [{ match_all: {} }], must_not: [] } }, + { + bool: { + filter: [], + minimum_should_match: 1, + must_not: [], + should: [{ term: { 'the-term': { value: 'the-value' } } }], + }, + }, { range: { 'the-time-field-name': { format: 'epoch_millis', - gte: 1577836800000, - lte: 1609459200000, + gte: 0, + lte: 50, }, }, }, @@ -42,7 +38,7 @@ describe('getHistogramQuery', () => { }); it('returns histogram query with additional filters', () => { - const query = getHistogramQuery(paramsMock, [ + const query = getHistogramQuery(paramsSearchQueryMock, [ { term: { ['the-filter-fieldName']: 'the-filter-fieldValue' }, }, @@ -50,7 +46,14 @@ describe('getHistogramQuery', () => { expect(query).toEqual({ bool: { filter: [ - { bool: { filter: [], must: [{ match_all: {} }], must_not: [] } }, + { + bool: { + filter: [], + minimum_should_match: 1, + must_not: [], + should: [{ term: { 'the-term': { value: 'the-value' } } }], + }, + }, { term: { 'the-filter-fieldName': 'the-filter-fieldValue', @@ -60,8 +63,8 @@ describe('getHistogramQuery', () => { range: { 'the-time-field-name': { format: 'epoch_millis', - gte: 1577836800000, - lte: 1609459200000, + gte: 0, + lte: 50, }, }, }, diff --git a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_query_with_params.test.ts b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_query_with_params.test.ts index ba62d87f6edb7b..cd43e031e916a5 100644 --- a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_query_with_params.test.ts +++ b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_query_with_params.test.ts @@ -5,34 +5,33 @@ * 2.0. */ +import { paramsMock } from './__mocks__/params_match_all'; +import { paramsSearchQueryMock } from './__mocks__/params_search_query'; + import { getQueryWithParams } from './get_query_with_params'; describe('getQueryWithParams', () => { it('returns the most basic query filtering', () => { const query = getQueryWithParams({ - params: { - index: 'the-index', - timeFieldName: 'the-time-field-name', - start: 1577836800000, - end: 1609459200000, - baselineMin: 10, - baselineMax: 20, - deviationMin: 30, - deviationMax: 40, - includeFrozen: false, - searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}', - }, + params: paramsSearchQueryMock, }); expect(query).toEqual({ bool: { filter: [ - { bool: { filter: [], must: [{ match_all: {} }], must_not: [] } }, + { + bool: { + filter: [], + minimum_should_match: 1, + must_not: [], + should: [{ term: { 'the-term': { value: 'the-value' } } }], + }, + }, { range: { 'the-time-field-name': { format: 'epoch_millis', - gte: 1577836800000, - lte: 1609459200000, + gte: 0, + lte: 50, }, }, }, @@ -43,18 +42,7 @@ describe('getQueryWithParams', () => { it('returns a query considering a custom field/value pair', () => { const query = getQueryWithParams({ - params: { - index: 'the-index', - timeFieldName: 'the-time-field-name', - start: 1577836800000, - end: 1609459200000, - baselineMin: 10, - baselineMax: 20, - deviationMin: 30, - deviationMax: 40, - includeFrozen: false, - searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}', - }, + params: paramsSearchQueryMock, termFilters: [ { fieldName: 'actualFieldName', @@ -65,13 +53,20 @@ describe('getQueryWithParams', () => { expect(query).toEqual({ bool: { filter: [ - { bool: { filter: [], must: [{ match_all: {} }], must_not: [] } }, + { + bool: { + filter: [], + minimum_should_match: 1, + must_not: [], + should: [{ term: { 'the-term': { value: 'the-value' } } }], + }, + }, { range: { 'the-time-field-name': { format: 'epoch_millis', - gte: 1577836800000, - lte: 1609459200000, + gte: 0, + lte: 50, }, }, }, @@ -87,18 +82,7 @@ describe('getQueryWithParams', () => { it("should not add `searchQuery` if it's just a match_all query", () => { const query = getQueryWithParams({ - params: { - index: 'the-index', - timeFieldName: 'the-time-field-name', - start: 1577836800000, - end: 1609459200000, - baselineMin: 10, - baselineMax: 20, - deviationMin: 30, - deviationMax: 40, - includeFrozen: false, - searchQuery: '{"match_all":{}}', - }, + params: paramsMock, }); expect(query).toEqual({ bool: { @@ -107,8 +91,8 @@ describe('getQueryWithParams', () => { range: { 'the-time-field-name': { format: 'epoch_millis', - gte: 1577836800000, - lte: 1609459200000, + gte: 0, + lte: 50, }, }, }, diff --git a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_request_base.test.ts b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_request_base.test.ts index ee21cb985f5d6e..33797e219fd372 100644 --- a/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_request_base.test.ts +++ b/x-pack/plugins/aiops/server/routes/log_rate_analysis/queries/get_request_base.test.ts @@ -5,36 +5,20 @@ * 2.0. */ +import { paramsMock } from './__mocks__/params_match_all'; + import { getRequestBase } from './get_request_base'; describe('getRequestBase', () => { it('defaults to not setting `ignore_throttled`', () => { - const requestBase = getRequestBase({ - index: 'the-index', - timeFieldName: 'the-time-field-name', - searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}', - start: 1577836800000, - end: 1609459200000, - baselineMin: 10, - baselineMax: 20, - deviationMin: 30, - deviationMax: 40, - }); + const requestBase = getRequestBase(paramsMock); expect(requestBase.ignore_throttled).toEqual(undefined); }); it('adds `ignore_throttled=false` when `includeFrozen=true`', () => { const requestBase = getRequestBase({ - index: 'the-index', - timeFieldName: 'the-time-field-name', + ...paramsMock, includeFrozen: true, - searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}', - start: 1577836800000, - end: 1609459200000, - baselineMin: 10, - baselineMax: 20, - deviationMin: 30, - deviationMax: 40, }); expect(requestBase.ignore_throttled).toEqual(false); });