From 51b5a7e9f96136e81bcb4ffcac8b9b43398b59ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Fri, 4 Apr 2025 17:18:53 +0200 Subject: [PATCH 1/3] [ES 9.0] (Remove body workaround) `@elastic/response-ops` --- .../lib/create_concrete_write_index.ts | 3 +-- .../create_or_update_component_template.ts | 20 ++++++++++--------- .../server/es/cluster_client_adapter.ts | 14 +++++-------- .../shared/task_manager/server/task_store.ts | 13 ++++++------ 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/x-pack/platform/plugins/shared/alerting/server/alerts_service/lib/create_concrete_write_index.ts b/x-pack/platform/plugins/shared/alerting/server/alerts_service/lib/create_concrete_write_index.ts index 84fe76d71aca2..8ecd2cfc89aca 100644 --- a/x-pack/platform/plugins/shared/alerting/server/alerts_service/lib/create_concrete_write_index.ts +++ b/x-pack/platform/plugins/shared/alerting/server/alerts_service/lib/create_concrete_write_index.ts @@ -90,8 +90,7 @@ const updateUnderlyingMapping = async ({ try { await retryTransientEsErrors( - // @ts-expect-error elasticsearch@9.0.0 https://github.com/elastic/elasticsearch-js/issues/2584 - () => esClient.indices.putMapping({ index, body: simulatedMapping }), + () => esClient.indices.putMapping({ index, ...simulatedMapping }), { logger } ); diff --git a/x-pack/platform/plugins/shared/alerting/server/alerts_service/lib/create_or_update_component_template.ts b/x-pack/platform/plugins/shared/alerting/server/alerts_service/lib/create_or_update_component_template.ts index 778795fd46eca..06675a1998bc8 100644 --- a/x-pack/platform/plugins/shared/alerting/server/alerts_service/lib/create_or_update_component_template.ts +++ b/x-pack/platform/plugins/shared/alerting/server/alerts_service/lib/create_or_update_component_template.ts @@ -50,17 +50,19 @@ const getIndexTemplatesUsingComponentTemplate = async ( () => esClient.indices.putIndexTemplate({ name: template.name, - body: { - ...template.index_template, - // @ts-expect-error elasticsearch@9.0.0 https://github.com/elastic/elasticsearch-js/issues/2584 - template: { - ...template.index_template.template, - settings: { - ...template.index_template.template?.settings, - 'index.mapping.total_fields.limit': totalFieldsLimit, - }, + ...template.index_template, + template: { + ...template.index_template.template, + settings: { + ...template.index_template.template?.settings, + 'index.mapping.total_fields.limit': totalFieldsLimit, }, }, + // GET brings string | string[] | undefined but this PUT expects string[] + ignore_missing_component_templates: template.index_template + .ignore_missing_component_templates + ? [template.index_template.ignore_missing_component_templates].flat() + : undefined, }), { logger } ); diff --git a/x-pack/platform/plugins/shared/event_log/server/es/cluster_client_adapter.ts b/x-pack/platform/plugins/shared/event_log/server/es/cluster_client_adapter.ts index 9f2a0809ee8f5..a9b895544e2a2 100644 --- a/x-pack/platform/plugins/shared/event_log/server/es/cluster_client_adapter.ts +++ b/x-pack/platform/plugins/shared/event_log/server/es/cluster_client_adapter.ts @@ -339,13 +339,10 @@ export class ClusterClientAdapter< const esClient = await this.elasticsearchClientPromise; await esClient.indices.putTemplate({ name: indexTemplateName, - body: { - ...currentIndexTemplate, - // @ts-expect-error elasticsearch@9.0.0 https://github.com/elastic/elasticsearch-js/issues/2584 - settings: { - ...currentIndexTemplate.settings, - 'index.hidden': true, - }, + ...currentIndexTemplate, + settings: { + ...currentIndexTemplate.settings, + 'index.hidden': true, }, }); } catch (err) { @@ -461,8 +458,7 @@ export class ClusterClientAdapter< const simulatedMapping = get(simulatedIndexMapping, ['template', 'mappings']); if (simulatedMapping != null) { - // @ts-expect-error elasticsearch@9.0.0 https://github.com/elastic/elasticsearch-js/issues/2584 - await esClient.indices.putMapping({ index: name, body: simulatedMapping }); + await esClient.indices.putMapping({ index: name, ...simulatedMapping }); this.logger.debug(`Successfully updated concrete index mappings for ${name}`); } } catch (err) { diff --git a/x-pack/platform/plugins/shared/task_manager/server/task_store.ts b/x-pack/platform/plugins/shared/task_manager/server/task_store.ts index 67fe82086daa6..02662a76faf21 100644 --- a/x-pack/platform/plugins/shared/task_manager/server/task_store.ts +++ b/x-pack/platform/plugins/shared/task_manager/server/task_store.ts @@ -903,8 +903,7 @@ export class TaskStore { ): Promise { const { query } = ensureQueryOnlyReturnsTaskObjects(opts); try { - const // @ts-expect-error elasticsearch@9.0.0 https://github.com/elastic/elasticsearch-js/issues/2584 types complain because the body should not be there. - // However, we can't use this API without the body because it fails to claim the tasks. + const // However, we can't use this API without the body because it fails to claim the tasks. // eslint-disable-next-line @typescript-eslint/naming-convention { total, updated, version_conflicts } = await this.esClientWithoutRetries.updateByQuery( { @@ -912,11 +911,11 @@ export class TaskStore { ignore_unavailable: true, refresh: true, conflicts: 'proceed', - body: { - ...opts, - max_docs, - query, - }, + ...opts, + max_docs, + query, + // Being explicit to work around an ES client type issue. + sort: opts.sort as string[] | undefined, }, { requestTimeout: this.requestTimeouts.update_by_query } ); From 9bc80b5a350951021957f2f4f8afded633736b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Mon, 7 Apr 2025 12:44:16 +0200 Subject: [PATCH 2/3] Fix tests --- .../alerts_service/alerts_service.test.ts | 14 ++++------ ...reate_or_update_component_template.test.ts | 28 ++++++++----------- .../server/es/cluster_client_adapter.test.ts | 18 ++++++------ .../shared/task_manager/server/task_store.ts | 8 ++++-- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.test.ts b/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.test.ts index 979acd67a2d73..77f01b32d5c01 100644 --- a/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.test.ts +++ b/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.test.ts @@ -473,14 +473,12 @@ describe('Alerts Service', () => { expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalledTimes(1); expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalledWith({ name: existingIndexTemplate.name, - body: { - ...existingIndexTemplate.index_template, - template: { - ...existingIndexTemplate.index_template.template, - settings: { - ...existingIndexTemplate.index_template.template?.settings, - 'index.mapping.total_fields.limit': 2500, - }, + ...existingIndexTemplate.index_template, + template: { + ...existingIndexTemplate.index_template.template, + settings: { + ...existingIndexTemplate.index_template.template?.settings, + 'index.mapping.total_fields.limit': 2500, }, }, }); diff --git a/x-pack/platform/plugins/shared/alerting/server/alerts_service/lib/create_or_update_component_template.test.ts b/x-pack/platform/plugins/shared/alerting/server/alerts_service/lib/create_or_update_component_template.test.ts index 0f53d1eadb976..ab7b13c822083 100644 --- a/x-pack/platform/plugins/shared/alerting/server/alerts_service/lib/create_or_update_component_template.test.ts +++ b/x-pack/platform/plugins/shared/alerting/server/alerts_service/lib/create_or_update_component_template.test.ts @@ -177,14 +177,12 @@ describe('createOrUpdateComponentTemplate', () => { expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalledTimes(1); expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalledWith({ name: existingIndexTemplate.name, - body: { - ...existingIndexTemplate.index_template, - template: { - ...existingIndexTemplate.index_template.template, - settings: { - ...existingIndexTemplate.index_template.template?.settings, - 'index.mapping.total_fields.limit': 2500, - }, + ...existingIndexTemplate.index_template, + template: { + ...existingIndexTemplate.index_template.template, + settings: { + ...existingIndexTemplate.index_template.template?.settings, + 'index.mapping.total_fields.limit': 2500, }, }, }); @@ -283,14 +281,12 @@ describe('createOrUpdateComponentTemplate', () => { expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalledTimes(1); expect(clusterClient.indices.putIndexTemplate).toHaveBeenCalledWith({ name: existingIndexTemplate.name, - body: { - ...existingIndexTemplate.index_template, - template: { - ...existingIndexTemplate.index_template.template, - settings: { - ...existingIndexTemplate.index_template.template?.settings, - 'index.mapping.total_fields.limit': 2500, - }, + ...existingIndexTemplate.index_template, + template: { + ...existingIndexTemplate.index_template.template, + settings: { + ...existingIndexTemplate.index_template.template?.settings, + 'index.mapping.total_fields.limit': 2500, }, }, }); diff --git a/x-pack/platform/plugins/shared/event_log/server/es/cluster_client_adapter.test.ts b/x-pack/platform/plugins/shared/event_log/server/es/cluster_client_adapter.test.ts index bdf72d753485a..c8695efc53a24 100644 --- a/x-pack/platform/plugins/shared/event_log/server/es/cluster_client_adapter.test.ts +++ b/x-pack/platform/plugins/shared/event_log/server/es/cluster_client_adapter.test.ts @@ -379,13 +379,11 @@ describe('setLegacyIndexTemplateToHidden', () => { await clusterClientAdapter.setLegacyIndexTemplateToHidden('foo-bar-template', currentTemplate); expect(clusterClient.indices.putTemplate).toHaveBeenCalledWith({ name: 'foo-bar-template', - body: { - order: 0, - index_patterns: ['foo-bar-*'], - settings: { index: { number_of_shards: '1' }, 'index.hidden': true }, - mappings: { dynamic: false, properties: {} }, - aliases: {}, - }, + order: 0, + index_patterns: ['foo-bar-*'], + settings: { index: { number_of_shards: '1' }, 'index.hidden': true }, + mappings: { dynamic: false, properties: {} }, + aliases: {}, }); }); @@ -659,7 +657,8 @@ describe('updateConcreteIndices', () => { }); expect(clusterClient.indices.putMapping).toHaveBeenCalledWith({ index: 'foo', - body: { dynamic: false, properties: { '@timestamp': { type: 'date' } } }, + dynamic: false, + properties: { '@timestamp': { type: 'date' } }, }); }); @@ -720,7 +719,8 @@ describe('updateConcreteIndices', () => { expect(clusterClient.indices.putMapping).toHaveBeenCalledWith({ index: 'foo', - body: { dynamic: false, properties: { '@timestamp': { type: 'date' } } }, + dynamic: false, + properties: { '@timestamp': { type: 'date' } }, }); expect(logger.error).toHaveBeenCalledWith( `Error updating index mappings for foo: failed to put mappings` diff --git a/x-pack/platform/plugins/shared/task_manager/server/task_store.ts b/x-pack/platform/plugins/shared/task_manager/server/task_store.ts index 02662a76faf21..31b62c8cb919a 100644 --- a/x-pack/platform/plugins/shared/task_manager/server/task_store.ts +++ b/x-pack/platform/plugins/shared/task_manager/server/task_store.ts @@ -902,6 +902,7 @@ export class TaskStore { { max_docs: max_docs }: UpdateByQueryOpts = {} ): Promise { const { query } = ensureQueryOnlyReturnsTaskObjects(opts); + const { sort, ...rest } = opts; try { const // However, we can't use this API without the body because it fails to claim the tasks. // eslint-disable-next-line @typescript-eslint/naming-convention @@ -911,11 +912,12 @@ export class TaskStore { ignore_unavailable: true, refresh: true, conflicts: 'proceed', - ...opts, + ...rest, max_docs, query, - // Being explicit to work around an ES client type issue. - sort: opts.sort as string[] | undefined, + // @ts-expect-error According to the docs, sort should be a comma-separated list of fields and goes in the querystring. + // However, this one is using a "body" format? + body: { sort }, }, { requestTimeout: this.requestTimeouts.update_by_query } ); From 2fd0409587e20bc93bb69779f0f8d8a7a1e8c61d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Mon, 7 Apr 2025 18:07:47 +0200 Subject: [PATCH 3/3] Remove comment left-over --- .../shared/task_manager/server/task_store.ts | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/x-pack/platform/plugins/shared/task_manager/server/task_store.ts b/x-pack/platform/plugins/shared/task_manager/server/task_store.ts index 31b62c8cb919a..d0e0b3776ba04 100644 --- a/x-pack/platform/plugins/shared/task_manager/server/task_store.ts +++ b/x-pack/platform/plugins/shared/task_manager/server/task_store.ts @@ -904,23 +904,22 @@ export class TaskStore { const { query } = ensureQueryOnlyReturnsTaskObjects(opts); const { sort, ...rest } = opts; try { - const // However, we can't use this API without the body because it fails to claim the tasks. - // eslint-disable-next-line @typescript-eslint/naming-convention - { total, updated, version_conflicts } = await this.esClientWithoutRetries.updateByQuery( - { - index: this.index, - ignore_unavailable: true, - refresh: true, - conflicts: 'proceed', - ...rest, - max_docs, - query, - // @ts-expect-error According to the docs, sort should be a comma-separated list of fields and goes in the querystring. - // However, this one is using a "body" format? - body: { sort }, - }, - { requestTimeout: this.requestTimeouts.update_by_query } - ); + // eslint-disable-next-line @typescript-eslint/naming-convention + const { total, updated, version_conflicts } = await this.esClientWithoutRetries.updateByQuery( + { + index: this.index, + ignore_unavailable: true, + refresh: true, + conflicts: 'proceed', + ...rest, + max_docs, + query, + // @ts-expect-error According to the docs, sort should be a comma-separated list of fields and goes in the querystring. + // However, this one is using a "body" format? + body: { sort }, + }, + { requestTimeout: this.requestTimeouts.update_by_query } + ); const conflictsCorrectedForContinuation = correctVersionConflictsForContinuation( updated,