# Templates w/ new java-client

**URL:** <https://discuss.elastic.co/t/templates-w-new-java-client/300218>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 21, 2022, 3:21pm UTC](https://discuss.elastic.co/t/templates-w-new-java-client/300218 "2022-03-21T15:21:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![divadpoc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divadpoc/32/97844_2.png) [@divadpoc](https://discuss.elastic.co/u/divadpoc)\
**Post date:** [March 21, 2022, 3:21pm UTC](https://discuss.elastic.co/t/templates-w-new-java-client/300218/1 "2022-03-21T15:21:14Z")

</div>

Initially I was looking for a way to add component and index templates via the new java-client. Then I stumbled upon the following two methods:

- `ElasticsearchIndicesClient.getTemplate`
- `ElasticsearchIndicesClient.getIndexTemplate`

both of their documentation links send me to the the [Index-Template](https://www.elastic.co/guide/en/elasticsearch/reference/master/index-templates.html) page.

Can you explain the difference of those two methods?

Anyhow, the component template is defined in the `cluster` namespace.

Back to my main problem: With the HLRC I was able to read a JSON from disk, defining the component or index template. But I can't seem to find it. In one Test I found [this method](https://github.com/elastic/elasticsearch-java/blob/main/java-client/src/test/java/co/elastic/clients/elasticsearch/spec_issues/SpecIssuesTest.java#L104) - is this the preferred way?

Thanks

---

<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 21, 2022, 3:51pm UTC](https://discuss.elastic.co/t/templates-w-new-java-client/300218/2 "2022-03-21T15:51:56Z")

</div>

The `getTemplate` method is for the ["legacy index template" API](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-template-v1.html). Thanks for the report, we will fix the link.

Regarding loading JSON from disk, this is indeed a way to do it. Be aware though that it may not work for requests that have a required path parameter, such as index creation. We have recently added a feature that allows reading any object from JSON and will be in the next minor releases.

Pull request: [Raw json support by swallez · Pull Request #200 · elastic/elasticsearch-java · GitHub](https://github.com/elastic/elasticsearch-java/pull/200)  
Docs: [Creating API objects from JSON data | Elasticsearch Java API Client [master] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/master/loading-json.html)

---

<div class="post-metadata">

**Author:** ![divadpoc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divadpoc/32/97844_2.png) [@divadpoc](https://discuss.elastic.co/u/divadpoc)\
**Post date:** [March 22, 2022, 8:02am UTC](https://discuss.elastic.co/t/templates-w-new-java-client/300218/3 "2022-03-22T08:02:50Z")

</div>

> [@swallez](#):
>
> The `getTemplate` method is for the ["legacy index template" API](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-template-v1.html). Thanks for the report, we will fix the link.

Perfect, and happy to help!.

> [@swallez](#):
>
> Regarding loading JSON from disk, this is indeed a way to do it. Be aware though that it may not work for requests that have a required path parameter, such as index creation. We have recently added a feature that allows reading any object from JSON and will be in the next minor releases.
> 
> Pull request: [Raw json support by swallez · Pull Request #200 · elastic/elasticsearch-java · GitHub](https://github.com/elastic/elasticsearch-java/pull/200)  
> Docs: [Creating API objects from JSON data | Elasticsearch Java API Client [master] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/master/loading-json.html)

I read the second link, but I need it for the IndexTemplate and ComponentTemplate to read from JSON, so I guess the pull request, if this includes those _configuration_ classes, should be perfect.

I'll go w/ the way in the Test class for now, and switch to the other one as soon as it's available.

Thanks.

---

<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 22, 2022, 3:05pm UTC](https://discuss.elastic.co/t/templates-w-new-java-client/300218/4 "2022-03-22T15:05:04Z")

</div>

> if this includes those _configuration_ classes, should be perfect

Yep, it does 🙂

---

<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 19, 2022, 3:05pm UTC](https://discuss.elastic.co/t/templates-w-new-java-client/300218/5 "2022-04-19T15:05:20Z")

</div>

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