Skip to content

Commit

Permalink
[Fleet] Generate dynamic template mappings for field with wildcard in…
Browse files Browse the repository at this point in the history
… name (elastic#137978)
  • Loading branch information
nchaulet authored Aug 3, 2022
1 parent 6bd3c73 commit 88eae3e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
46 changes: 46 additions & 0 deletions x-pack/plugins/fleet/server/services/epm/fields/field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
]
}
]
`);
});
});
17 changes: 16 additions & 1 deletion x-pack/plugins/fleet/server/services/epm/fields/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 88eae3e

Please sign in to comment.