How to create index template
in ElsaticSearch 7.17 or later
using Java API Client
?
I decided not to use the Java High Level REST Client
because it was marked as deprecated.
However, no reference materials were found anywhere for generating index templates using the Java API Client
.
In particular, I don't know how to set the mappings
.
I tried as below, the index template
was created, but the mappings
are displayed as empty.
Map<String, JsonData> settings = new HashMap<>();
settings.put("number_of_shards", JsonData.of(1));
settings.put("number_of_replicas", JsonData.of(0));
settings.put("index", JsonData.of(Json.createObjectBuilder()
.add("lifecycle", Json.createObjectBuilder()
.add("name", ilmPolicyName)
.add("rollover_alias", indexAlias)
).build()));
JsonpMapper mapper = elasticsearchClient._transport().jsonpMapper();
String json = Files.readString(Path.of("src/main/resources/static/indexTemplate.json"));
JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
elasticsearchClient.indices().putTemplate(PutTemplateRequest.of(b -> b
.name(indexTemplateName)
.indexPatterns(List.of(indexPattern))
.settings(settings)
.mappings(TypeMapping._DESERIALIZER.deserialize(parser, mapper))
));
What's wrong?