Skip to content

Commit 76c55a2

Browse files
authored
[Fleet] Support dynamic_template mappings from object field (#137772)
1 parent 755fad1 commit 76c55a2

9 files changed

Lines changed: 456 additions & 119 deletions

File tree

x-pack/plugins/fleet/common/types/models/epm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ export type PackageAssetReference = Pick<SavedObjectReference, 'id'> & {
483483

484484
export interface IndexTemplateMappings {
485485
properties: any;
486+
dynamic_templates?: any;
486487
}
487488

488489
// This is an index template v2, see https://github.com/elastic/elasticsearch/issues/53101

x-pack/plugins/fleet/server/services/epm/elasticsearch/template/__snapshots__/template.test.ts.snap

Lines changed: 83 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 2.0.
66
*/
77

8-
import { merge } from 'lodash';
8+
import { merge, concat, uniqBy, omit } from 'lodash';
99
import Boom from '@hapi/boom';
1010
import type { ElasticsearchClient, Logger } from '@kbn/core/server';
1111

@@ -241,6 +241,15 @@ function buildComponentTemplates(params: {
241241

242242
const templateSettings = merge(defaultSettings, indexTemplateSettings);
243243

244+
const indexTemplateMappings = registryElasticsearch?.['index_template.mappings'] ?? {};
245+
246+
const mappingsProperties = merge(mappings.properties, indexTemplateMappings.properties ?? {});
247+
248+
const mappingsDynamicTemplates = uniqBy(
249+
concat(mappings.dynamic_templates ?? [], indexTemplateMappings.dynamic_templates ?? []),
250+
(dynampingTemplate) => Object.keys(dynampingTemplate)[0]
251+
);
252+
244253
templatesMap[packageTemplateName] = {
245254
template: {
246255
settings: {
@@ -256,7 +265,11 @@ function buildComponentTemplates(params: {
256265
},
257266
},
258267
},
259-
mappings: merge(mappings, registryElasticsearch?.['index_template.mappings'] ?? {}),
268+
mappings: {
269+
properties: mappingsProperties,
270+
dynamic_templates: mappingsDynamicTemplates.length ? mappingsDynamicTemplates : undefined,
271+
...omit(indexTemplateMappings, 'properties', 'dynamic_templates'),
272+
},
260273
},
261274
_meta,
262275
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import type { Field } from '../../fields/field';
9+
10+
const DEFAULT_SCALING_FACTOR = 1000;
11+
12+
interface Properties {
13+
[key: string]: any;
14+
}
15+
16+
export function getDefaultProperties(field: Field): Properties {
17+
const properties: Properties = {};
18+
19+
if (field.index !== undefined) {
20+
properties.index = field.index;
21+
}
22+
if (field.doc_values !== undefined) {
23+
properties.doc_values = field.doc_values;
24+
}
25+
if (field.copy_to) {
26+
properties.copy_to = field.copy_to;
27+
}
28+
29+
return properties;
30+
}
31+
32+
export function scaledFloat(field: Field): Properties {
33+
const fieldProps = getDefaultProperties(field);
34+
fieldProps.type = 'scaled_float';
35+
fieldProps.scaling_factor = field.scaling_factor || DEFAULT_SCALING_FACTOR;
36+
if (field.metric_type) {
37+
fieldProps.time_series_metric = field.metric_type;
38+
}
39+
40+
return fieldProps;
41+
}
42+
43+
export function histogram(field: Field): Properties {
44+
const fieldProps = getDefaultProperties(field);
45+
fieldProps.type = 'histogram';
46+
47+
return fieldProps;
48+
}

x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ describe('EPM template', () => {
137137
expect(mappings).toMatchSnapshot(path.basename(ymlPath));
138138
});
139139

140+
it('tests loading cockroachdb_dynamic_templates.yml', () => {
141+
const ymlPath = path.join(__dirname, '../../fields/tests/cockroachdb_dynamic_templates.yml');
142+
const fieldsYML = readFileSync(ymlPath, 'utf-8');
143+
const fields: Field[] = safeLoad(fieldsYML);
144+
const processedFields = processFields(fields);
145+
146+
const mappings = generateMappings(processedFields);
147+
148+
expect(mappings).toMatchSnapshot(path.basename(ymlPath));
149+
});
150+
140151
it('tests processing long field with index false', () => {
141152
const longWithIndexFalseYml = `
142153
- name: longIndexFalse

0 commit comments

Comments
 (0)