diff --git a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/__snapshots__/template.test.ts.snap b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/__snapshots__/template.test.ts.snap index 3e4c9659892b2..758d0e6d1bc11 100644 --- a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/__snapshots__/template.test.ts.snap +++ b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/__snapshots__/template.test.ts.snap @@ -58,25 +58,10 @@ exports[`EPM template tests loading base.yml: base.yml 1`] = ` exports[`EPM template tests loading cockroachdb_dynamic_templates.yml: cockroachdb_dynamic_templates.yml 1`] = ` { - "properties": { - "cockroachdb": { - "properties": { - "status": { - "properties": { - "labels": { - "properties": {} - }, - "*": { - "properties": {} - } - } - } - } - } - }, + "properties": {}, "dynamic_templates": [ { - "cockroachdb.status.labels.*": { + "cockroachdb.status.labels": { "mapping": { "type": "keyword" }, @@ -1403,9 +1388,6 @@ exports[`EPM template tests loading system.yml: system.yml 1`] = ` }, "failed": { "type": "long" - }, - "states": { - "properties": {} } } }, @@ -1463,9 +1445,6 @@ exports[`EPM template tests loading system.yml: system.yml 1`] = ` } } }, - "user": { - "properties": {} - }, "summary": { "properties": { "all": { 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 de31cb51bb7a1..9f1503acd5175 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 @@ -158,7 +158,11 @@ function _generateMappings( addDynamicMapping: any; groupFieldName?: string; } -): IndexTemplateMappings { +): { + properties: IndexTemplateMappings['properties']; + hasNonDynamicTemplateMappings: boolean; +} { + let hasNonDynamicTemplateMappings = false; const props: Properties = {}; // TODO: this can happen when the fields property in fields.yml is present but empty // Maybe validation should be moved to fields/field.ts @@ -213,24 +217,29 @@ function _generateMappings( switch (type) { case 'group': + const mappings = _generateMappings(field.fields!, { + ...ctx, + groupFieldName: ctx.groupFieldName + ? `${ctx.groupFieldName}.${field.name}` + : field.name, + }); + if (!mappings.hasNonDynamicTemplateMappings) { + return; + } + fieldProps = { - ..._generateMappings(field.fields!, { - ...ctx, - groupFieldName: ctx.groupFieldName - ? `${ctx.groupFieldName}.${field.name}` - : field.name, - }), + properties: mappings.properties, ...generateDynamicAndEnabled(field), }; break; case 'group-nested': fieldProps = { - ..._generateMappings(field.fields!, { + properties: _generateMappings(field.fields!, { ...ctx, groupFieldName: ctx.groupFieldName ? `${ctx.groupFieldName}.${field.name}` : field.name, - }), + }).properties, ...generateNestedProps(field), type: 'nested', }; @@ -309,11 +318,12 @@ function _generateMappings( } props[field.name] = fieldProps; + hasNonDynamicTemplateMappings = true; } }); } - return { properties: props }; + return { properties: props, hasNonDynamicTemplateMappings }; } function generateDynamicAndEnabled(field: Field) { diff --git a/x-pack/plugins/fleet/server/services/epm/fields/tests/cockroachdb_dynamic_templates.yml b/x-pack/plugins/fleet/server/services/epm/fields/tests/cockroachdb_dynamic_templates.yml index 5fa4ff937bd14..d9ab38e07d7d6 100644 --- a/x-pack/plugins/fleet/server/services/epm/fields/tests/cockroachdb_dynamic_templates.yml +++ b/x-pack/plugins/fleet/server/services/epm/fields/tests/cockroachdb_dynamic_templates.yml @@ -2,7 +2,7 @@ type: group release: beta fields: - - name: labels.* + - name: labels type: object object_type: keyword description: >