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 c06d0cdbb6429..dbc93e35c8218 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 @@ -1900,9 +1900,19 @@ 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' }], + data_streams: [ + { + name: 'test-constant.keyword-default', + indices: [ + { index_name: '.ds-test-constant.keyword-default-0001' }, + { index_name: '.ds-test-constant.keyword-default-0002' }, + ], + }, + ], } as any); + esClient.indices.get.mockResponse({ 'test-constant.keyword-default': { mappings: { @@ -1942,6 +1952,9 @@ describe('EPM template', () => { } as any, }, ]); + expect(esClient.indices.get).toBeCalledWith({ + index: '.ds-test-constant.keyword-default-0002', + }); const putMappingsCalls = esClient.indices.putMapping.mock.calls; expect(putMappingsCalls).toHaveLength(1); expect(putMappingsCalls[0][0]).toEqual({ 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 b9c0846f3e4f2..bb38480fd66b7 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 @@ -65,6 +65,7 @@ export interface CurrentDataStream { dataStreamName: string; replicated: boolean; indexTemplate: IndexTemplate; + currentWriteIndex: string; } const DEFAULT_IGNORE_ABOVE = 1024; @@ -954,6 +955,7 @@ const getDataStreams = async ( dataStreamName: dataStream.name, replicated: dataStream.replicated, indexTemplate, + currentWriteIndex: dataStream.indices?.at(-1)?.index_name, })); }; @@ -989,6 +991,7 @@ const updateAllDataStreams = async ( return updateExistingDataStream({ esClient, logger, + currentWriteIndex: templateEntry.currentWriteIndex, dataStreamName: templateEntry.dataStreamName, options, }); @@ -1002,11 +1005,13 @@ const updateAllDataStreams = async ( const updateExistingDataStream = async ({ dataStreamName, + currentWriteIndex, esClient, logger, options, }: { dataStreamName: string; + currentWriteIndex: string; esClient: ElasticsearchClient; logger: Logger; options?: { @@ -1015,7 +1020,7 @@ const updateExistingDataStream = async ({ }; }) => { const existingDs = await esClient.indices.get({ - index: dataStreamName, + index: currentWriteIndex, }); const existingDsConfig = Object.values(existingDs);