# Migration HLRC PutIndexTemplateRequest to ES 8.1.0

**URL:** <https://discuss.elastic.co/t/migration-hlrc-putindextemplaterequest-to-es-8-1-0/300491>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 23, 2022, 6:14pm UTC](https://discuss.elastic.co/t/migration-hlrc-putindextemplaterequest-to-es-8-1-0/300491 "2022-03-23T18:14:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raushan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raushan/32/102180_2.png) [@Raushan](https://discuss.elastic.co/u/Raushan)\
**Post date:** [March 23, 2022, 6:14pm UTC](https://discuss.elastic.co/t/migration-hlrc-putindextemplaterequest-to-es-8-1-0/300491/1 "2022-03-23T18:14:34Z")

</div>

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

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

```

where templateJson is

```auto
{
   "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)](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.1.0/co/elastic/clients/elasticsearch/indices/PutIndexTemplateRequest.html)

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**

```auto
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());

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 24, 2022, 8:16am UTC](https://discuss.elastic.co/t/migration-hlrc-putindextemplaterequest-to-es-8-1-0/300491/2 "2022-03-24T08:16:52Z")

</div>

What about this example?

```java
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)))
)));

```

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [March 24, 2022, 11:07am UTC](https://discuss.elastic.co/t/migration-hlrc-putindextemplaterequest-to-es-8-1-0/300491/3 "2022-03-24T11:07:32Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.1/loading-json.html)

Hope this helps.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 21, 2022, 11:08am UTC](https://discuss.elastic.co/t/migration-hlrc-putindextemplaterequest-to-es-8-1-0/300491/4 "2022-04-21T11:08:00Z")

</div>

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