forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] [ML] Fix query for pattern analysis and change point analysis (e…
…lastic#194742) (elastic#195246) # Backport This will backport the following commits from `main` to `8.x`: - [[ML] Fix query for pattern analysis and change point analysis (elastic#194742)](elastic#194742) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"James Gowdy","email":"jgowdy@elastic.co"},"sourceCommit":{"committedDate":"2024-10-07T12:41:50Z","message":"[ML] Fix query for pattern analysis and change point analysis (elastic#194742)\n\nFixes https://github.com/elastic/kibana/issues/190710\r\n\r\nAdds an additional check for `query_string` to the query creating\r\nfunction to adjust the query if only a single `query_string` condition\r\nis being used.\r\nThis function was originally only used for pattern analysis, but has\r\nbeen renamed and moved to a common location so change point analysis can\r\nalso use it.","sha":"02f277efa7470035671f67d4e86e74ea5f52838e","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix",":ml","v9.0.0","v8.16.0","backport:version"],"title":"[ML] Fix query for pattern analysis and change point analysis","number":194742,"url":"https://github.com/elastic/kibana/pull/194742","mergeCommit":{"message":"[ML] Fix query for pattern analysis and change point analysis (elastic#194742)\n\nFixes https://github.com/elastic/kibana/issues/190710\r\n\r\nAdds an additional check for `query_string` to the query creating\r\nfunction to adjust the query if only a single `query_string` condition\r\nis being used.\r\nThis function was originally only used for pattern analysis, but has\r\nbeen renamed and moved to a common location so change point analysis can\r\nalso use it.","sha":"02f277efa7470035671f67d4e86e74ea5f52838e"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/194742","number":194742,"mergeCommit":{"message":"[ML] Fix query for pattern analysis and change point analysis (elastic#194742)\n\nFixes https://github.com/elastic/kibana/issues/190710\r\n\r\nAdds an additional check for `query_string` to the query creating\r\nfunction to adjust the query if only a single `query_string` condition\r\nis being used.\r\nThis function was originally only used for pattern analysis, but has\r\nbeen renamed and moved to a common location so change point analysis can\r\nalso use it.","sha":"02f277efa7470035671f67d4e86e74ea5f52838e"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: James Gowdy <jgowdy@elastic.co>
- Loading branch information
1 parent
c099f33
commit 1963138
Showing
6 changed files
with
177 additions
and
24 deletions.
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
x-pack/packages/ml/aiops_common/create_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,156 @@ | ||
/* | ||
* 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 { createDefaultQuery } from './create_default_query'; | ||
|
||
describe('createDefaultQuery', () => { | ||
it('should create a default match_all query when no input query is provided', () => { | ||
const result = createDefaultQuery(undefined, 'timestamp', undefined); | ||
expect(result).toEqual({ | ||
bool: { | ||
must: [{ match_all: {} }], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should wrap an existing match_all query in a bool must clause', () => { | ||
const inputQuery = { match_all: {} }; | ||
const result = createDefaultQuery(inputQuery, 'timestamp', undefined); | ||
expect(result).toEqual({ | ||
bool: { | ||
must: [{ match_all: {} }], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should wrap an existing query_string query in a bool must clause', () => { | ||
const inputQuery = { query_string: { query: '*' } }; | ||
const result = createDefaultQuery(inputQuery, 'timestamp', undefined); | ||
expect(result).toEqual({ | ||
bool: { | ||
must: [{ query_string: { query: '*' } }], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should wrap an existing multi_match query in a bool should clause', () => { | ||
const inputQuery = { multi_match: { query: 'test', fields: ['field1', 'field2'] } }; | ||
const result = createDefaultQuery(inputQuery, 'timestamp', undefined); | ||
expect(result).toEqual({ | ||
bool: { | ||
must: [], | ||
should: { multi_match: { query: 'test', fields: ['field1', 'field2'] } }, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should add a time range filter to the query', () => { | ||
const timeRange = { from: 1609459200000, to: 1609545600000 }; | ||
const result = createDefaultQuery(undefined, 'timestamp', timeRange); | ||
expect(result).toEqual({ | ||
bool: { | ||
must: [ | ||
{ match_all: {} }, | ||
{ | ||
range: { | ||
timestamp: { | ||
gte: 1609459200000, | ||
lte: 1609545600000, | ||
format: 'epoch_millis', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should merge existing bool query with new time range filter', () => { | ||
const inputQuery = { bool: { must: [{ term: { field: 'value' } }] } }; | ||
const timeRange = { from: 1609459200000, to: 1609545600000 }; | ||
const result = createDefaultQuery(inputQuery, 'timestamp', timeRange); | ||
expect(result).toEqual({ | ||
bool: { | ||
must: [ | ||
{ term: { field: 'value' } }, | ||
{ | ||
range: { | ||
timestamp: { | ||
gte: 1609459200000, | ||
lte: 1609545600000, | ||
format: 'epoch_millis', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should handle an existing bool query with must clause', () => { | ||
const inputQuery = { bool: { must: [{ term: { field: 'value' } }] } }; | ||
const result = createDefaultQuery(inputQuery, 'timestamp', undefined); | ||
expect(result).toEqual({ | ||
bool: { | ||
must: [{ term: { field: 'value' } }], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should handle an existing bool query with should clause', () => { | ||
const inputQuery = { bool: { should: [{ term: { field: 'value' } }] } }; | ||
const result = createDefaultQuery(inputQuery, 'timestamp', undefined); | ||
expect(result).toEqual({ | ||
bool: { | ||
must: [], | ||
should: [{ term: { field: 'value' } }], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should handle an existing bool query with must_not clause', () => { | ||
const inputQuery = { bool: { must_not: [{ term: { field: 'value' } }] } }; | ||
const result = createDefaultQuery(inputQuery, 'timestamp', undefined); | ||
expect(result).toEqual({ | ||
bool: { | ||
must: [], | ||
must_not: [{ term: { field: 'value' } }], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should handle an existing bool query with filter clause', () => { | ||
const inputQuery = { bool: { filter: [{ term: { field: 'value' } }] } }; | ||
const result = createDefaultQuery(inputQuery, 'timestamp', undefined); | ||
expect(result).toEqual({ | ||
bool: { | ||
must: [], | ||
filter: [{ term: { field: 'value' } }], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should handle an input query with multiple clauses', () => { | ||
const inputQuery = { | ||
bool: { | ||
must: [{ term: { field1: 'value1' } }], | ||
should: [{ term: { field2: 'value2' } }], | ||
must_not: [{ term: { field3: 'value3' } }], | ||
filter: [{ term: { field4: 'value4' } }], | ||
}, | ||
}; | ||
const result = createDefaultQuery(inputQuery, 'timestamp', undefined); | ||
expect(result).toEqual({ | ||
bool: { | ||
must: [{ term: { field1: 'value1' } }], | ||
should: [{ term: { field2: 'value2' } }], | ||
must_not: [{ term: { field3: 'value3' } }], | ||
filter: [{ term: { field4: 'value4' } }], | ||
}, | ||
}); | ||
}); | ||
}); |
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
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 |
---|---|---|
|
@@ -23,5 +23,6 @@ | |
"@kbn/saved-search-plugin", | ||
"@kbn/data-views-plugin", | ||
"@kbn/ml-is-populated-object", | ||
"@kbn/aiops-common", | ||
] | ||
} |
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