Skip to content

Commit

Permalink
[ML] Anomaly Detection: Update annotation directly using the index it…
Browse files Browse the repository at this point in the history
… is stored in. (#126573) (#126678)

Fixes the approach used to update an annotation so that it is update using the index that it is stored in.

(cherry picked from commit 31939f0)

# Conflicts:
#	x-pack/plugins/ml/server/models/annotation_service/annotation.ts
  • Loading branch information
walterra authored Mar 2, 2022
1 parent 2f4480c commit 68a2c39
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
8 changes: 4 additions & 4 deletions x-pack/plugins/ml/common/types/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

// The Annotation interface is based on annotation documents stored in the
// `.ml-annotations-6` index, accessed via the `.ml-annotations-[read|write]` aliases.
// `.ml-annotations-*` index, accessed via the `.ml-annotations-[read|write]` aliases.

// Annotation document mapping:
// PUT .ml-annotations-6
// PUT .ml-annotations-000001
// {
// "mappings": {
// "annotation": {
Expand Down Expand Up @@ -54,8 +54,8 @@
// POST /_aliases
// {
// "actions" : [
// { "add" : { "index" : ".ml-annotations-6", "alias" : ".ml-annotations-read" } },
// { "add" : { "index" : ".ml-annotations-6", "alias" : ".ml-annotations-write" } }
// { "add" : { "index" : ".ml-annotations-000001", "alias" : ".ml-annotations-read" } },
// { "add" : { "index" : ".ml-annotations-000001", "alias" : ".ml-annotations-write" } }
// ]
// }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"max_score": 0,
"hits": [
{
"_index": ".ml-annotations-6",
"_type": "doc",
"_index": ".ml-annotations-000001",
"_id": "T-CNvmgBQUJYQVn7TCPA",
"_score": 0,
"_source": {
Expand All @@ -32,8 +31,7 @@
}
},
{
"_index": ".ml-annotations-6",
"_type": "doc",
"_index": ".ml-annotations-000001",
"_id": "3lVpvmgB5xYzd3PM-MSe",
"_score": 0,
"_source": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('annotation_service', () => {

const annotationMockId = 'mockId';
const deleteParamsMock: DeleteParams = {
index: '.ml-annotations-6',
index: '.ml-annotations-000001',
id: annotationMockId,
refresh: 'wait_for',
};
Expand Down
50 changes: 28 additions & 22 deletions x-pack/plugins/ml/server/models/annotation_service/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ export interface DeleteParams {
}

export function annotationProvider({ asInternalUser }: IScopedClusterClient) {
// Find the index the annotation is stored in.
async function fetchAnnotationIndex(id: string) {
const searchParams: estypes.SearchRequest = {
index: ML_ANNOTATIONS_INDEX_ALIAS_READ,
size: 1,
body: {
query: {
ids: {
values: [id],
},
},
},
};

const { body } = await asInternalUser.search(searchParams);
const totalCount =
typeof body.hits.total === 'number' ? body.hits.total : body.hits.total!.value;

if (totalCount === 0) {
throw Boom.notFound(`Cannot find annotation with ID ${id}`);
}

return body.hits.hits[0]._index;
}

async function indexAnnotation(annotation: Annotation, username: string) {
if (isAnnotation(annotation) === false) {
// No need to translate, this will not be exposed in the UI.
Expand All @@ -95,6 +120,8 @@ export function annotationProvider({ asInternalUser }: IScopedClusterClient) {

if (typeof annotation._id !== 'undefined') {
params.id = annotation._id;
params.index = await fetchAnnotationIndex(annotation._id);
params.require_alias = false;
delete params.body._id;
delete params.body.key;
}
Expand Down Expand Up @@ -385,28 +412,7 @@ export function annotationProvider({ asInternalUser }: IScopedClusterClient) {
}

async function deleteAnnotation(id: string) {
// Find the index the annotation is stored in.
const searchParams: estypes.SearchRequest = {
index: ML_ANNOTATIONS_INDEX_ALIAS_READ,
size: 1,
body: {
query: {
ids: {
values: [id],
},
},
},
};

const { body } = await asInternalUser.search(searchParams);
const totalCount =
typeof body.hits.total === 'number' ? body.hits.total : body.hits.total.value;

if (totalCount === 0) {
throw Boom.notFound(`Cannot find annotation with ID ${id}`);
}

const index = body.hits.hits[0]._index;
const index = await fetchAnnotationIndex(id);

const deleteParams: DeleteParams = {
index,
Expand Down

0 comments on commit 68a2c39

Please sign in to comment.