Reputation: 23237
My code is:
PutIndexTemplateRequest ngramTemplate = new PutIndexTemplateRequest("ngram-template")
.patterns(Arrays.asList("resourcetable-*", "termconcept-*"))
.settings(Settings.builder().put("index.max_ngram_diff", 50));
RestHighLevelClient elasticsearchHighLevelRestClient = ElasticsearchRestClientFactory.createElasticsearchHighLevelRestClient(qualifiedHost, port, theUsername, thePassword);
AcknowledgedResponse acknowledgedResponse = elasticsearchHighLevelRestClient.indices().putTemplate(ngramTemplate, RequestOptions.DEFAULT);
assert acknowledgedResponse.isAcknowledged();
This code is perfermod without problems.
However, when I'm trying to list all my index templates, my index template doesn't appear.
$ curl -s http://localhost:9200/_index_template`
{
"index_templates": [
{
"name": "metrics",
"index_template": {
"index_patterns": [
"metrics-*-*"
],
"composed_of": [
"metrics-mappings",
"metrics-settings"
],
"priority": 100,
"version": 0,
"_meta": {
"managed": true,
"description": "default metrics template installed by x-pack"
},
"data_stream": {}
}
},
{
"name": "logs",
"index_template": {
"index_patterns": [
"logs-*-*"
],
"composed_of": [
"logs-mappings",
"logs-settings"
],
"priority": 100,
"version": 0,
"_meta": {
"managed": true,
"description": "default logs template installed by x-pack"
},
"data_stream": {}
}
}
]
}
Any ideas?
Upvotes: 1
Views: 1704
Reputation: 4410
I had the same problem using the Rest High Level Client for Elasticsearch version 7.10.2.
When I logged the URL of the request, I could see that PutIndexTemplateRequest
uses the API _template
instead of _index_template
which apparently gets ignored when the index is created later via CreateIndexRequest
.
The solution was to use PutComposableIndexTemplateRequest:
PutComposableIndexTemplateRequest request =
new PutComposableIndexTemplateRequest().name(INDEX_TEMPLATE_NAME);
Settings settings = Settings.builder()
.loadFromSource(MY_SETTINGS_JSON_STRING, XContentType.JSON).build();
Template template = new Template(settings, null, null);
ComposableIndexTemplate composableIndexTemplate =
new ComposableIndexTemplate(List.of("my-index-pattern"),
template, null, null, null, null);
request.indexTemplate(composableIndexTemplate);
try {
client.indices().putIndexTemplate(request, RequestOptions.DEFAULT);
}
} catch (IOException e) {
// handle error
}
Upvotes: 1