[Fleet] Generate dynamic template mappings for field with wildcard in name (#137978) (#138025)

(cherry picked from commit 88eae3ef40)

Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
This commit is contained in:
Kibana Machine 2022-08-03 15:34:26 -04:00 committed by GitHub
parent b242ba6b8d
commit 2e81f4c1ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 1 deletions

View file

@ -624,4 +624,50 @@ describe('processFields', () => {
];
expect(processFields(fields)).toEqual(fieldsExpected);
});
test('handle wildcard field', () => {
const wildcardFields = [
{
name: 'a.*.b',
type: 'keyword',
},
{
name: 'a.b.*',
type: 'scaled_float',
},
];
expect(processFields(wildcardFields)).toMatchInlineSnapshot(`
[
{
"name": "a",
"type": "group",
"fields": [
{
"name": "*",
"type": "group",
"fields": [
{
"name": "b",
"type": "object",
"object_type": "keyword"
}
]
},
{
"name": "b",
"type": "group",
"fields": [
{
"name": "*",
"type": "object",
"object_type": "scaled_float"
}
]
}
]
}
]
`);
});
});

View file

@ -246,8 +246,23 @@ export const getField = (fields: Fields, pathNames: string[]): Field | undefined
return undefined;
};
export function processFieldsWithWildcard(fields: Fields): Fields {
const newFields: Fields = [];
for (const field of fields) {
const hasWildcard = field.name.includes('*');
const hasObjectType = field.object_type;
if (hasWildcard && !hasObjectType) {
newFields.push({ ...field, type: 'object', object_type: field.type });
} else {
newFields.push({ ...field });
}
}
return newFields;
}
export function processFields(fields: Fields): Fields {
const expandedFields = expandFields(fields);
const processedFields = processFieldsWithWildcard(fields);
const expandedFields = expandFields(processedFields);
const dedupedFields = dedupFields(expandedFields);
return validateFields(dedupedFields, dedupedFields);
}