-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ML] AIOps: Improve cleanup of default queries. #176534
Merged
walterra
merged 14 commits into
elastic:main
from
walterra:ml-176387-fix-functional-tests
Mar 8, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4eb9895
assert url state after reload in functional tests
walterra ba76eb8
filter default queries
walterra f7367b8
remove commented code
walterra 7c2342a
Merge branch 'main' into ml-176387-fix-functional-tests
walterra c31895e
add JSDoc
walterra a6fd23a
Merge branch 'main' into ml-176387-fix-functional-tests
walterra d2fe25e
Merge branch 'main' into ml-176387-fix-functional-tests
walterra 9a6c5c7
isDefaultSimplyQuery
walterra aa908b2
simplify isFilterBasedDefaultQuery
walterra 6e6d393
update jest tests
walterra f9a1e31
improve comments
walterra f24f256
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine 263a9e8
fix export, update comments
walterra 0fda433
Merge branch 'ml-176387-fix-functional-tests' of github.com:walterra/…
walterra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
x-pack/packages/ml/query_utils/src/__mocks__/simple_query.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* 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 { SearchQueryVariant } from '../types'; | ||
|
||
export const simpleQueryMock: SearchQueryVariant = { query_string: { query: 'airline:AAL' } }; |
63 changes: 63 additions & 0 deletions
63
x-pack/packages/ml/query_utils/src/bool_filter_based_simple_query.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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 { isBoolFilterBasedSimpleQuery } from './bool_filter_based_simple_query'; | ||
import { simpleQueryMock } from './__mocks__/simple_query'; | ||
import { matchAllQuery } from './match_all_query'; | ||
import { defaultSimpleQuery } from './simple_query'; | ||
|
||
describe('isBoolFilterBasedSimpleQuery', () => { | ||
it('should identify bool filter based simple queries', () => { | ||
expect( | ||
isBoolFilterBasedSimpleQuery({ | ||
bool: { filter: [simpleQueryMock] }, | ||
}) | ||
).toBe(true); | ||
|
||
expect( | ||
isBoolFilterBasedSimpleQuery({ | ||
bool: { filter: [simpleQueryMock], must: [], must_not: [], should: [] }, | ||
}) | ||
).toBe(true); | ||
}); | ||
|
||
it('should identify non-simple queries or differently structured simple queries', () => { | ||
expect(isBoolFilterBasedSimpleQuery(defaultSimpleQuery)).toBe(false); | ||
expect(isBoolFilterBasedSimpleQuery(matchAllQuery)).toBe(false); | ||
expect(isBoolFilterBasedSimpleQuery(simpleQueryMock)).toBe(false); | ||
|
||
expect( | ||
isBoolFilterBasedSimpleQuery({ | ||
bool: { filter: [], must: [], must_not: [], should: [] }, | ||
}) | ||
).toBe(false); | ||
|
||
expect( | ||
isBoolFilterBasedSimpleQuery({ | ||
bool: { filter: [] }, | ||
}) | ||
).toBe(false); | ||
|
||
expect( | ||
isBoolFilterBasedSimpleQuery({ | ||
bool: { filter: [matchAllQuery], must: [], must_not: [], should: [] }, | ||
}) | ||
).toBe(false); | ||
|
||
expect( | ||
isBoolFilterBasedSimpleQuery({ | ||
bool: { filter: [matchAllQuery] }, | ||
}) | ||
).toBe(false); | ||
|
||
expect( | ||
isBoolFilterBasedSimpleQuery({ | ||
bool: { filter: [], must: [matchAllQuery], must_not: [] }, | ||
}) | ||
).toBe(false); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
x-pack/packages/ml/query_utils/src/bool_filter_based_simple_query.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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 { isPopulatedObject } from '@kbn/ml-is-populated-object'; | ||
|
||
import { isSimpleQuery } from './simple_query'; | ||
import type { BoolFilterBasedSimpleQuery } from './types'; | ||
|
||
/** | ||
* Type guard to check if the provided argument is a boolean filter-based simple query. | ||
* | ||
* A valid `BoolFilterBasedSimpleQuery` must have a `bool` property, which itself | ||
* must have a `filter` property. This `filter` must be an array with exactly | ||
* one element, and that element must satisfy the conditions of a simple query | ||
* as defined by `isSimpleQuery`. | ||
* | ||
* The type guard is useful to identify simple queries within bool filter | ||
* queries exposed from Kibana/EUI search bars. | ||
* | ||
* @param arg - The argument to be checked. | ||
* @returns `true` if `arg` meets the criteria of a `BoolFilterBasedSimpleQuery`, otherwise `false`. | ||
*/ | ||
export function isBoolFilterBasedSimpleQuery(arg: unknown): arg is BoolFilterBasedSimpleQuery { | ||
return ( | ||
isPopulatedObject(arg, ['bool']) && | ||
isPopulatedObject(arg.bool, ['filter']) && | ||
Array.isArray(arg.bool.filter) && | ||
arg.bool.filter.length === 1 && | ||
isSimpleQuery(arg.bool.filter[0]) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
*/ | ||
|
||
import { simpleQueryMock } from './__mocks__/simple_query'; | ||
import { isDefaultQuery } from './default_query'; | ||
import { matchAllQuery } from './match_all_query'; | ||
import { defaultSimpleQuery } from './simple_query'; | ||
|
||
describe('isDefaultQuery', () => { | ||
it("should return if it's a default query", () => { | ||
expect(isDefaultQuery(defaultSimpleQuery)).toBe(true); | ||
expect(isDefaultQuery(matchAllQuery)).toBe(true); | ||
expect(isDefaultQuery(simpleQueryMock)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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 { isBoolFilterBasedSimpleQuery } from './bool_filter_based_simple_query'; | ||
import { isMatchAllQuery } from './match_all_query'; | ||
import { isSimpleDefaultQuery } from './simple_query'; | ||
import type { SearchQueryVariant } from './types'; | ||
|
||
/** | ||
* Checks if the provided query is a default query. A default query is considered as one that matches all documents, | ||
* either directly through a `match_all` query, a `SimpleQuery` with a wildcard query string, or a `BoolFilterBasedSimpleQuery` | ||
* that contains a filter with a wildcard query or a `match_all` condition. | ||
* | ||
* @param query - The query to check. | ||
* @returns True if the query is a default query, false otherwise. | ||
*/ | ||
export function isDefaultQuery(query: SearchQueryVariant): boolean { | ||
return ( | ||
isMatchAllQuery(query) || | ||
isSimpleDefaultQuery(query) || | ||
(isBoolFilterBasedSimpleQuery(query) && | ||
(query.bool.filter[0].query_string.query === '*' || isMatchAllQuery(query.bool.filter[0]))) | ||
); | ||
} |
52 changes: 52 additions & 0 deletions
52
x-pack/packages/ml/query_utils/src/filter_based_default_query.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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 { isFilterBasedDefaultQuery } from './filter_based_default_query'; | ||
import { simpleQueryMock } from './__mocks__/simple_query'; | ||
import { matchAllQuery } from './match_all_query'; | ||
import { defaultSimpleQuery } from './simple_query'; | ||
|
||
describe('isFilterBasedDefaultQuery', () => { | ||
it('should identify filter based default queries', () => { | ||
expect( | ||
isFilterBasedDefaultQuery({ | ||
bool: { filter: [], must: [], must_not: [], should: [] }, | ||
}) | ||
).toBe(true); | ||
expect( | ||
isFilterBasedDefaultQuery({ | ||
bool: { filter: [matchAllQuery], must: [], must_not: [], should: [] }, | ||
}) | ||
).toBe(true); | ||
expect( | ||
isFilterBasedDefaultQuery({ | ||
bool: { filter: [], must: [matchAllQuery], must_not: [] }, | ||
}) | ||
).toBe(true); | ||
}); | ||
|
||
it('should identify non-default queries', () => { | ||
expect(isFilterBasedDefaultQuery(defaultSimpleQuery)).toBe(false); | ||
expect(isFilterBasedDefaultQuery(matchAllQuery)).toBe(false); | ||
expect(isFilterBasedDefaultQuery(simpleQueryMock)).toBe(false); | ||
expect( | ||
isFilterBasedDefaultQuery({ | ||
bool: { filter: [simpleQueryMock], must: [], must_not: [], should: [] }, | ||
}) | ||
).toBe(false); | ||
expect( | ||
isFilterBasedDefaultQuery({ | ||
bool: { filter: [], must: [matchAllQuery], must_not: [], should: [simpleQueryMock] }, | ||
}) | ||
).toBe(false); | ||
expect( | ||
isFilterBasedDefaultQuery({ | ||
bool: { filter: [], must: [matchAllQuery], must_not: [simpleQueryMock] }, | ||
}) | ||
).toBe(false); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
x-pack/packages/ml/query_utils/src/filter_based_default_query.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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 { isPopulatedObject } from '@kbn/ml-is-populated-object'; | ||
|
||
import { isDefaultQuery } from './default_query'; | ||
|
||
const boolRequiredAttributes = ['filter', 'must', 'must_not']; | ||
|
||
/** | ||
* Determines if the provided argument is a filter-based default query within a boolean filter context. | ||
* | ||
* A valid filter-based default query must include a `bool` property that contains | ||
* `filter`, `must`, and `must_not` properties. These properties should either be empty arrays | ||
* or arrays containing exactly one default query. The function checks for these conditions | ||
* to identify variants of default queries structured within a boolean filter. | ||
* | ||
* Examples of valid structures include: | ||
* - `{ bool: { filter: [{ match_all: {} }], must: [], must_not: [], should: [] } }` | ||
* - `{ bool: { filter: [], must: [{ match_all: {} }], must_not: [] } }` | ||
* | ||
* Useful to identify simple queries within bool queries | ||
* exposed from Kibana/EUI search bars. | ||
* | ||
* @param arg - The argument to be checked, its structure is unknown upfront. | ||
* @returns Returns `true` if `arg` matches the expected structure of a | ||
* filter-based default query, otherwise `false`. | ||
*/ | ||
export function isFilterBasedDefaultQuery(arg: unknown): boolean { | ||
return ( | ||
isPopulatedObject(arg, ['bool']) && | ||
isPopulatedObject(arg.bool, boolRequiredAttributes) && | ||
Object.values(arg.bool).every( | ||
// should be either an empty array or an array with just 1 default query | ||
(d) => { | ||
return Array.isArray(d) && (d.length === 0 || (d.length === 1 && isDefaultQuery(d[0]))); | ||
} | ||
) | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
x-pack/packages/ml/query_utils/src/match_all_query.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { isMatchAllQuery, matchAllQuery } from './match_all_query'; | ||
|
||
describe('isMatchAllQuery', () => { | ||
it("should return if it's a match_all query", () => { | ||
expect(isMatchAllQuery(matchAllQuery)).toBe(true); | ||
expect(isMatchAllQuery({ query_string: { query: '*' } })).toBe(false); | ||
expect(isMatchAllQuery({ query_string: { query: 'airline:AAL' } })).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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 { isPopulatedObject } from '@kbn/ml-is-populated-object'; | ||
|
||
/** | ||
* Represents a query that matches all documents. | ||
*/ | ||
export const matchAllQuery = { | ||
/** | ||
* 'match_all' property specifies a query that matches all documents. | ||
*/ | ||
match_all: {}, | ||
}; | ||
|
||
/** | ||
* Checks if an argument is a `match_all` query. | ||
* @param {unknown} query - Argument to check. | ||
* @returns {boolean} True if `query` is a `match_all` query, false otherwise. | ||
*/ | ||
export function isMatchAllQuery(query: unknown): boolean { | ||
return ( | ||
isPopulatedObject(query, ['match_all']) && | ||
typeof query.match_all === 'object' && | ||
query.match_all !== null && | ||
Object.keys(query.match_all).length === 0 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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 { simpleQueryMock } from './__mocks__/simple_query'; | ||
import { defaultSimpleQuery, isSimpleQuery, isSimpleDefaultQuery } from './simple_query'; | ||
import { matchAllQuery } from './match_all_query'; | ||
|
||
describe('isSimpleQuery', () => { | ||
it("should return if it's a simple query", () => { | ||
expect(isSimpleQuery(defaultSimpleQuery)).toBe(true); | ||
expect(isSimpleQuery(matchAllQuery)).toBe(false); | ||
expect(isSimpleQuery(simpleQueryMock)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('isSimpleDefaultQuery', () => { | ||
it("should return if it's a simple default query", () => { | ||
expect(isSimpleDefaultQuery(defaultSimpleQuery)).toBe(true); | ||
expect(isSimpleDefaultQuery(matchAllQuery)).toBe(false); | ||
expect(isSimpleDefaultQuery(simpleQueryMock)).toBe(false); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would separate
should identify filter-based default queries
andshould identify non-default queries
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 updated in 6e6d393.