Skip to content

Commit

Permalink
field alias testing
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Dec 9, 2024
1 parent 2d37926 commit 3eeeabe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion x-pack/plugins/streams/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export type ProcessingDefinition = z.infer<typeof processingDefinitionSchema>;

export const fieldDefinitionSchema = z.object({
name: z.string(),
type: z.enum(['keyword', 'match_only_text', 'long', 'double', 'date', 'boolean', 'ip']),
type: z.enum(['keyword', 'match_only_text', 'long', 'double', 'date', 'boolean', 'ip', 'alias']),
alias_path: z.optional(z.string()),
});

export type FieldDefinition = z.infer<typeof fieldDefinitionSchema>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import {
ClusterPutComponentTemplateRequest,
MappingDateProperty,
MappingFieldAliasProperty,
MappingProperty,
} from '@elastic/elasticsearch/lib/api/types';
import { StreamDefinition } from '../../../../common/types';
Expand All @@ -18,7 +19,8 @@ import { getComponentTemplateName } from './name';

export function generateLayer(
id: string,
definition: StreamDefinition
definition: StreamDefinition,
rootDefinition: StreamDefinition | undefined
): ClusterPutComponentTemplateRequest {
const properties: Record<string, MappingProperty> = {};
definition.fields.forEach((field) => {
Expand All @@ -29,6 +31,18 @@ export function generateLayer(
// @timestamp can't ignore malformed dates as it's used for sorting in logsdb
(property as MappingDateProperty).ignore_malformed = false;
}
if (field.type === 'alias' && field.alias_path) {
(property as MappingFieldAliasProperty).path = field.alias_path;
// try to find the referenced field in the root definition and add it here as well to satisfy the Elasticsearch alias checker
if (rootDefinition) {
const rootField = rootDefinition.fields.find((f) => f.name === field.alias_path);
if (rootField) {
properties[field.alias_path] = {
type: rootField.type,
};
}
}
}
properties[field.name] = property;
});
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ export const rootStreamDefinition: StreamDefinition = {
name: 'message',
type: 'match_only_text',
},
{
name: 'body.text',
type: 'match_only_text',
},
{
name: 'host.name',
type: 'keyword',
},
{
name: 'resource.attributes.host.name',
type: 'keyword',
},
{
name: 'log.level',
type: 'keyword',
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/streams/server/lib/streams/stream_crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ export async function validateAncestorFields(
});
for (const ancestor of ancestors) {
for (const field of fields) {
if (field.type === 'alias') {
// TODO: validate whether field under alias_path has the same type as an ancestor field definition of the current field
continue;
}
if (
ancestor.definition.fields.some(
(ancestorField) => ancestorField.type !== field.type && ancestorField.name === field.name
Expand Down Expand Up @@ -455,7 +459,7 @@ export async function syncStream({
});
return;
}
const componentTemplate = generateLayer(definition.id, definition);
const componentTemplate = generateLayer(definition.id, definition, rootDefinition);
await upsertComponent({
esClient: scopedClusterClient.asCurrentUser,
logger,
Expand Down

0 comments on commit 3eeeabe

Please sign in to comment.