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.
HLRC Code

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

where templateJson is

{
   "index_patterns":"appcache*",
   "settings":{
      "number_of_shards":2,
      "max_result_window":2147483647,
      "mapping.total_fields.limit":2000000
   },
   "mappings":{
      "dynamic":"false",
      "properties":{
         "key":{
            "properties":{
               "entityId":{
                  "type":"long"
               },
               "entityNr":{
                  "type":"long"
               },
               "sessionId":{
                  "type":"long"
               }
            }
         },
         "timeOfCreation":{
            "index":true,
            "type":"date",
            "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
                            .dynamic(DynamicMapping.False)
                    ).build();
            PutIndexTemplateResponse putIndexTemplateResponse =
                    esClient.indices().putIndexTemplate(new PutIndexTemplateRequest.Builder()
                            .indexPatterns("appcache*")
                            .template(indexTemplateMapping)
                            .name(templateName).build());

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.