From 4589dd90e105cb68b6e192c0a3f2c61b37493ecd Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Tue, 2 Aug 2022 16:25:02 -0400 Subject: [PATCH] Fix merge existing dynamic_templates --- .../epm/elasticsearch/template/install.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts index 91f291d25d6be..c39366edab519 100644 --- a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { merge } from 'lodash'; +import { merge, concat, uniqBy, omit } from 'lodash'; import Boom from '@hapi/boom'; import type { ElasticsearchClient, Logger } from '@kbn/core/server'; @@ -241,6 +241,15 @@ function buildComponentTemplates(params: { const templateSettings = merge(defaultSettings, indexTemplateSettings); + const indexTemplateMappings = registryElasticsearch?.['index_template.mappings'] ?? {}; + + const mappingsProperties = merge(mappings.properties, indexTemplateMappings.properties ?? {}); + + const mappingsDynamicTemplates = uniqBy( + concat(mappings.dynamic_templates ?? [], indexTemplateMappings.dynamic_templates ?? []), + (dynampingTemplate) => Object.keys(dynampingTemplate)[0] + ); + templatesMap[packageTemplateName] = { template: { settings: { @@ -256,7 +265,11 @@ function buildComponentTemplates(params: { }, }, }, - mappings: merge(mappings, registryElasticsearch?.['index_template.mappings'] ?? {}), + mappings: { + properties: mappingsProperties, + dynamic_templates: mappingsDynamicTemplates.length ? mappingsDynamicTemplates : undefined, + ...omit(indexTemplateMappings, 'properties', 'dynamic_templates'), + }, }, _meta, };