Skip to content

Commit

Permalink
[Fleet] Support dynamic_template mappings from object field
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Aug 2, 2022
1 parent e264a7f commit 3fad1e9
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 111 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
@@ -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 3fad1e9

Please sign in to comment.