search-backend-module-elasticsearch: add support for reading index templates from options

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-08-09 11:29:39 +02:00
parent b7e0362496
commit b67f413882
2 changed files with 55 additions and 2 deletions
+28 -1
View File
@@ -43,6 +43,33 @@ export interface Config {
*/
fragmentDelimiter?: string;
};
/** Elasticsearch specific index template bodies */
indexTemplates?: Array<{
name: string;
body: {
/**
* Array of wildcard (*) expressions used to match the names of data streams and indices during creation.
*/
index_patterns: string[];
/**
* An ordered list of component template names.
* Component templates are merged in the order specified,
* meaning that the last component template specified has the highest precedence.
*/
composed_of?: string[];
/**
* See available properties of template
* https://www.elastic.co/guide/en/elasticsearch/reference/7.15/indices-put-template.html#put-index-template-api-request-body
*/
template?: {
[key: string]: unknown;
};
};
}>;
/** Miscellaneous options for the client */
clientOptions?: {
ssl?: {
@@ -128,7 +155,7 @@ export interface Config {
};
/* TODO(kuangp): unsupported until @elastic/elasticsearch@7.14 is released
| {
/**
* Bearer authentication token to connect to the cluster.
* See: https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-service-token.html
@@ -165,7 +165,7 @@ export class ElasticSearchSearchEngine implements SearchEngine {
logger.info('Initializing ElasticSearch search engine.');
}
return new ElasticSearchSearchEngine(
const engine = new ElasticSearchSearchEngine(
clientOptions,
aliasPostfix,
indexPrefix,
@@ -176,6 +176,14 @@ export class ElasticSearchSearchEngine implements SearchEngine {
'search.elasticsearch.highlightOptions',
),
);
for (const indexTemplate of this.readIndexTemplateConfig(
config.getConfig('search.elasticsearch'),
)) {
await engine.setIndexTemplate(indexTemplate);
}
return engine;
}
/**
@@ -518,6 +526,24 @@ export class ElasticSearchSearchEngine implements SearchEngine {
: {}),
};
}
private static readIndexTemplateConfig(
config: Config,
): ElasticSearchCustomIndexTemplate[] {
return (
config.getOptionalConfigArray('indexTemplates')?.map(templateConfig => {
const bodyConfig = templateConfig.getConfig('body');
return {
name: templateConfig.getString('name'),
body: {
index_patterns: bodyConfig.getStringArray('index_patterns'),
composed_of: bodyConfig.getOptionalStringArray('composed_of'),
template: bodyConfig.getOptionalConfig('template')?.get(),
},
};
}) ?? []
);
}
}
/**