From 4773f525586a05e58ca82cffaa66c89c7b0df4c1 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Sat, 14 Sep 2024 13:58:08 +1000 Subject: [PATCH] [8.x] [UII] Make constant keyword backfill optional (#192921) (#192940) # Backport This will backport the following commits from `main` to `8.x`: - [[UII] Make constant keyword backfill optional (#192921)](https://github.com/elastic/kibana/pull/192921) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Jen Huang --- .../elasticsearch/template/template.test.ts | 104 ++++++++++++++++++ .../epm/elasticsearch/template/template.ts | 14 ++- 2 files changed, 114 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.test.ts b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.test.ts index 989c28e205052..371ea4a470eaf 100644 --- a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.test.ts +++ b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.test.ts @@ -1867,6 +1867,110 @@ describe('EPM template', () => { }, }); }); + + it('should fill constant keywords from previous mappings', async () => { + const esClient = elasticsearchServiceMock.createElasticsearchClient(); + esClient.indices.getDataStream.mockResponse({ + data_streams: [{ name: 'test-constant.keyword-default' }], + } as any); + esClient.indices.get.mockResponse({ + 'test-constant.keyword-default': { + mappings: { + properties: { + some_keyword_field: { + type: 'constant_keyword', + }, + }, + }, + }, + } as any); + esClient.indices.simulateTemplate.mockResponse({ + template: { + settings: { index: {} }, + mappings: { + properties: { + some_keyword_field: { + type: 'constant_keyword', + value: 'some_value', + }, + }, + }, + }, + } as any); + const logger = loggerMock.create(); + await updateCurrentWriteIndices(esClient, logger, [ + { + templateName: 'test', + indexTemplate: { + index_patterns: ['test-constant.keyword-*'], + template: { + template: { + settings: { index: {} }, + mappings: { properties: {} }, + }, + }, + } as any, + }, + ]); + const putMappingsCalls = esClient.indices.putMapping.mock.calls; + expect(putMappingsCalls).toHaveLength(1); + expect(putMappingsCalls[0][0]).toEqual({ + index: 'test-constant.keyword-default', + body: { + properties: { + some_keyword_field: { + type: 'constant_keyword', + value: 'some_value', + }, + }, + }, + write_index_only: true, + }); + }); + + it('should not error when previous mappings are not found', async () => { + const esClient = elasticsearchServiceMock.createElasticsearchClient(); + esClient.indices.getDataStream.mockResponse({ + data_streams: [{ name: 'test-constant.keyword-default' }], + } as any); + esClient.indices.get.mockResponse({ + 'test-constant.keyword-default': { + mappings: { + properties: { + some_keyword_field: { + type: 'constant_keyword', + }, + }, + }, + }, + } as any); + esClient.indices.simulateTemplate.mockResponse({ + template: {}, + } as any); + const logger = loggerMock.create(); + await updateCurrentWriteIndices(esClient, logger, [ + { + templateName: 'test', + indexTemplate: { + index_patterns: ['test-constant.keyword-*'], + template: { + template: { + settings: { index: {} }, + mappings: { properties: {} }, + }, + }, + } as any, + }, + ]); + const putMappingsCalls = esClient.indices.putMapping.mock.calls; + expect(putMappingsCalls).toHaveLength(1); + expect(putMappingsCalls[0][0]).toEqual({ + index: 'test-constant.keyword-default', + body: {}, + write_index_only: true, + }); + }); + it('should rollover on expected error', async () => { const esClient = elasticsearchServiceMock.createElasticsearchClient(); esClient.indices.getDataStream.mockResponse({ diff --git a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.ts b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.ts index efd621e9cc63a..3709975c57a5e 100644 --- a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.ts +++ b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.ts @@ -1027,10 +1027,16 @@ const updateExistingDataStream = async ({ ); settings = simulateResult.template.settings; - mappings = fillConstantKeywordValues( - currentBackingIndexConfig?.mappings || {}, - simulateResult.template.mappings - ); + + try { + mappings = fillConstantKeywordValues( + currentBackingIndexConfig?.mappings || {}, + simulateResult.template.mappings || {} + ); + } catch (err) { + logger.error(`Error filling constant keyword values: ${err}`); + mappings = simulateResult.template.mappings; + } lifecycle = simulateResult.template.lifecycle;