Skip to content

Commit

Permalink
[Fleet] Support dynamic_template mappings from object field (#137772)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Aug 3, 2022
1 parent 755fad1 commit 76c55a2
Show file tree
Hide file tree
Showing 9 changed files with 456 additions and 119 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ export type PackageAssetReference = Pick<SavedObjectReference, 'id'> & {

export interface IndexTemplateMappings {
properties: any;
dynamic_templates?: any;
}

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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: {
Expand All @@ -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,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { Field } from '../../fields/field';

const DEFAULT_SCALING_FACTOR = 1000;

interface Properties {
[key: string]: any;
}

export function getDefaultProperties(field: Field): Properties {
const properties: Properties = {};

if (field.index !== undefined) {
properties.index = field.index;
}
if (field.doc_values !== undefined) {
properties.doc_values = field.doc_values;
}
if (field.copy_to) {
properties.copy_to = field.copy_to;
}

return properties;
}

export function scaledFloat(field: Field): Properties {
const fieldProps = getDefaultProperties(field);
fieldProps.type = 'scaled_float';
fieldProps.scaling_factor = field.scaling_factor || DEFAULT_SCALING_FACTOR;
if (field.metric_type) {
fieldProps.time_series_metric = field.metric_type;
}

return fieldProps;
}

export function histogram(field: Field): Properties {
const fieldProps = getDefaultProperties(field);
fieldProps.type = 'histogram';

return fieldProps;
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ describe('EPM template', () => {
expect(mappings).toMatchSnapshot(path.basename(ymlPath));
});

it('tests loading cockroachdb_dynamic_templates.yml', () => {
const ymlPath = path.join(__dirname, '../../fields/tests/cockroachdb_dynamic_templates.yml');
const fieldsYML = readFileSync(ymlPath, 'utf-8');
const fields: Field[] = safeLoad(fieldsYML);
const processedFields = processFields(fields);

const mappings = generateMappings(processedFields);

expect(mappings).toMatchSnapshot(path.basename(ymlPath));
});

it('tests processing long field with index false', () => {
const longWithIndexFalseYml = `
- name: longIndexFalse
Expand Down
Loading

0 comments on commit 76c55a2

Please sign in to comment.