Migration HLRC PutIndexTemplateRequest to ES 8.1.0

Hi, I'm migrating from HLRC to ES8.1. We used to pass json data for settings and mappings in source.

            PutIndexTemplateRequest request = new PutIndexTemplateRequest(templateName);
            request.source(templateJson, XContentType.JSON);
            putTemplateResponse = restClient.indices().putTemplate(request, RequestOptions.DEFAULT);

where templateJson is

            "format":"yyyy-MM-dd'T'HH:mm:ss.SSSZ||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"

There is no option to add json data as per doc PutIndexTemplateRequest (java-client 8.1.0 API)

So i tried to breakdown json, but were unable to build properties, Is there any reference for the same, or any other way to build?

ES 8.1.0 Client

IndexTemplateMapping indexTemplateMapping = new IndexTemplateMapping.Builder()
                    .settings(s -> s.shards(2).maxResultWindow(2147483647).maxScriptFields(2000000))
                    .mappings(m -> m
                            //.properties unable to structure properties for json
            PutIndexTemplateResponse putIndexTemplateResponse =
                    esClient.indices().putIndexTemplate(new PutIndexTemplateRequest.Builder()

What about this example?

client.indices().putIndexTemplate(b -> b.template(t -> t.mappings(m -> m
            .properties("my_field", Property.of(pb -> pb.keyword(kb -> kb)))
            .properties("my_long", Property.of(pb -> pb.long_(lb -> lb)))

We added a feature in 8.1.1 for this purpose. You can fill almost any data structure of the Java API client using the new withJson method. Please see the docs at Creating API objects from JSON data | Elasticsearch Java API Client [8.1] | Elastic

Hope this helps.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.