Skip to content

Commit f8041e6

Browse files
[ML] Delete annotation directly from the index it is stored in (#115328)
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent c1b0565 commit f8041e6

4 files changed

Lines changed: 32 additions & 16 deletions

File tree

x-pack/plugins/ml/common/constants/index_patterns.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
export const ML_ANNOTATIONS_INDEX_ALIAS_READ = '.ml-annotations-read';
99
export const ML_ANNOTATIONS_INDEX_ALIAS_WRITE = '.ml-annotations-write';
10-
export const ML_ANNOTATIONS_INDEX_PATTERN = '.ml-annotations-6';
1110

1211
export const ML_RESULTS_INDEX_PATTERN = '.ml-anomalies-*';
1312
export const ML_NOTIFICATION_INDEX_PATTERN = '.ml-notifications*';

x-pack/plugins/ml/server/lib/check_annotations/index.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,15 @@ import { mlLog } from '../../lib/log';
1111
import {
1212
ML_ANNOTATIONS_INDEX_ALIAS_READ,
1313
ML_ANNOTATIONS_INDEX_ALIAS_WRITE,
14-
ML_ANNOTATIONS_INDEX_PATTERN,
1514
} from '../../../common/constants/index_patterns';
1615

1716
// Annotations Feature is available if:
18-
// - ML_ANNOTATIONS_INDEX_PATTERN index is present
1917
// - ML_ANNOTATIONS_INDEX_ALIAS_READ alias is present
2018
// - ML_ANNOTATIONS_INDEX_ALIAS_WRITE alias is present
19+
// Note there is no need to check for the existence of the indices themselves as aliases are stored
20+
// in the metadata of the indices they point to, so it's impossible to have an alias that doesn't point to any index.
2121
export async function isAnnotationsFeatureAvailable({ asInternalUser }: IScopedClusterClient) {
2222
try {
23-
const indexParams = { index: ML_ANNOTATIONS_INDEX_PATTERN };
24-
25-
const { body: annotationsIndexExists } = await asInternalUser.indices.exists(indexParams);
26-
if (!annotationsIndexExists) {
27-
return false;
28-
}
29-
3023
const { body: annotationsReadAliasExists } = await asInternalUser.indices.existsAlias({
3124
index: ML_ANNOTATIONS_INDEX_ALIAS_READ,
3225
name: ML_ANNOTATIONS_INDEX_ALIAS_READ,

x-pack/plugins/ml/server/models/annotation_service/annotation.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import getAnnotationsRequestMock from './__mocks__/get_annotations_request.json'
99
import getAnnotationsResponseMock from './__mocks__/get_annotations_response.json';
1010

1111
import { ANNOTATION_TYPE } from '../../../common/constants/annotations';
12-
import { ML_ANNOTATIONS_INDEX_ALIAS_WRITE } from '../../../common/constants/index_patterns';
1312
import { Annotation, isAnnotations } from '../../../common/types/annotations';
1413

1514
import { DeleteParams, GetResponse, IndexAnnotationArgs } from './annotation';
@@ -42,7 +41,7 @@ describe('annotation_service', () => {
4241

4342
const annotationMockId = 'mockId';
4443
const deleteParamsMock: DeleteParams = {
45-
index: ML_ANNOTATIONS_INDEX_ALIAS_WRITE,
44+
index: '.ml-annotations-6',
4645
id: annotationMockId,
4746
refresh: 'wait_for',
4847
};

x-pack/plugins/ml/server/models/annotation_service/annotation.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export interface IndexParams {
7171
index: string;
7272
body: Annotation;
7373
refresh: boolean | 'wait_for' | undefined;
74+
require_alias?: boolean;
7475
id?: string;
7576
}
7677

@@ -99,6 +100,7 @@ export function annotationProvider({ asInternalUser }: IScopedClusterClient) {
99100
index: ML_ANNOTATIONS_INDEX_ALIAS_WRITE,
100101
body: annotation,
101102
refresh: 'wait_for',
103+
require_alias: true,
102104
};
103105

104106
if (typeof annotation._id !== 'undefined') {
@@ -407,14 +409,37 @@ export function annotationProvider({ asInternalUser }: IScopedClusterClient) {
407409
}
408410

409411
async function deleteAnnotation(id: string) {
410-
const params: DeleteParams = {
411-
index: ML_ANNOTATIONS_INDEX_ALIAS_WRITE,
412+
// Find the index the annotation is stored in.
413+
const searchParams: estypes.SearchRequest = {
414+
index: ML_ANNOTATIONS_INDEX_ALIAS_READ,
415+
size: 1,
416+
body: {
417+
query: {
418+
ids: {
419+
values: [id],
420+
},
421+
},
422+
},
423+
};
424+
425+
const { body } = await asInternalUser.search(searchParams);
426+
const totalCount =
427+
typeof body.hits.total === 'number' ? body.hits.total : body.hits.total.value;
428+
429+
if (totalCount === 0) {
430+
throw Boom.notFound(`Cannot find annotation with ID ${id}`);
431+
}
432+
433+
const index = body.hits.hits[0]._index;
434+
435+
const deleteParams: DeleteParams = {
436+
index,
412437
id,
413438
refresh: 'wait_for',
414439
};
415440

416-
const { body } = await asInternalUser.delete(params);
417-
return body;
441+
const { body: deleteResponse } = await asInternalUser.delete(deleteParams);
442+
return deleteResponse;
418443
}
419444

420445
return {

0 commit comments

Comments
 (0)