# Add componentTemplate with mapping with new Java client

**URL:** <https://discuss.elastic.co/t/add-componenttemplate-with-mapping-with-new-java-client/356252>\
**Category:** Elasticsearch\
**Created:** [March 27, 2024, 9:32am UTC](https://discuss.elastic.co/t/add-componenttemplate-with-mapping-with-new-java-client/356252 "2024-03-27T09:32:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![avnere](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@avnere](https://discuss.elastic.co/u/avnere)\
**Post date:** [March 27, 2024, 9:32am UTC](https://discuss.elastic.co/t/add-componenttemplate-with-mapping-with-new-java-client/356252/1 "2024-03-27T09:32:53Z")

</div>

Hi,

I have started using new Java client. I want to add new componentTemplate with mapping. What is the approach with the new client?

This was the approach on 7.17:

[java-rest-high-put-index-template-v2.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-put-index-template-v2.html)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 27, 2024, 10:10am UTC](https://discuss.elastic.co/t/add-componenttemplate-with-mapping-with-new-java-client/356252/2 "2024-03-27T10:10:35Z")

</div>

Something like:

```auto
client.cluster().putComponentTemplate(pct -> pct
        .name("my_component_template")
        .template(t -> t
                .settings(s -> s.numberOfShards("1").numberOfReplicas("0"))
                .mappings(m -> m
                        .properties("foo", p -> p.text(tp -> tp))
                )
        )
);

```

---

<div class="post-metadata">

**Author:** ![avnere](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@avnere](https://discuss.elastic.co/u/avnere)\
**Post date:** [March 27, 2024, 11:12am UTC](https://discuss.elastic.co/t/add-componenttemplate-with-mapping-with-new-java-client/356252/3 "2024-03-27T11:12:41Z")

</div>

Thanks for fast response.

Is there an option to provide the mapping as JSON (like we had in v.7)

```auto
Template template = new Template(
					null,
					new CompressedXContent(mapping),
					null);

			ComponentTemplate ct = new ComponentTemplate(
					template,
					version,
					null);

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 27, 2024, 2:00pm UTC](https://discuss.elastic.co/t/add-componenttemplate-with-mapping-with-new-java-client/356252/4 "2024-03-27T14:00:12Z")

</div>

This should do it.

```auto
PutComponentTemplateResponse response = client.cluster().putComponentTemplate(pct -> pct
        .name("my_component_template")
        .template(t -> t
                .withJson(new StringReader("{\n" +
                        " \"mappings\": {\n" +
                        " \"properties\": {\n" +
                        " \"@timestamp\": {\n" +
                        " \"type\": \"date\"\n" +
                        " }\n" +
                        " }\n" +
                        " }\n" +
                        " }"))
        )
);

```

---

<div class="post-metadata">

**Author:** ![avnere](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@avnere](https://discuss.elastic.co/u/avnere)\
**Post date:** [March 27, 2024, 2:19pm UTC](https://discuss.elastic.co/t/add-componenttemplate-with-mapping-with-new-java-client/356252/5 "2024-03-27T14:19:41Z")

</div>

Is there any docs page for new client?  
Like you had in v.7: [java-rest-high-put-index-template-v2](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-put-index-template-v2.html)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 27, 2024, 2:33pm UTC](https://discuss.elastic.co/t/add-componenttemplate-with-mapping-with-new-java-client/356252/6 "2024-03-27T14:33:28Z")

</div>

Sadly no.

But you have some advices from the doc: [API conventions | Elasticsearch Java API Client [8.13] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/api-conventions.html)

And my blog post: [Switching from the Java High Level Rest Client to the new Java API Client | Elastic Blog](https://www.elastic.co/blog/switching-from-the-java-high-level-rest-client-to-the-new-java-api-client)

And my "demo" repo:

> <https://github.com/dadoonet/elasticsearch-java-client-demo/blob/main/src/test/java/fr/pilato/test/elasticsearch/hlclient/EsClientIT.java>

---

<div class="post-metadata">

**Author:** ![avnere](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@avnere](https://discuss.elastic.co/u/avnere)\
**Post date:** [March 27, 2024, 3:35pm UTC](https://discuss.elastic.co/t/add-componenttemplate-with-mapping-with-new-java-client/356252/7 "2024-03-27T15:35:25Z")

</div>

Can you also show how to create index template with settings, alias and componentTemplate?

Like: [put-index-template](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-put-index-template-v2.html#_settings_2)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 27, 2024, 4:25pm UTC](https://discuss.elastic.co/t/add-componenttemplate-with-mapping-with-new-java-client/356252/8 "2024-03-27T16:25:37Z")

</div>

Should be straightforward:

```auto
client.cluster().putComponentTemplate(pct -> pct
        .name("my_component_template")
        .template(t -> t
                .settings(s -> s.numberOfShards("1").numberOfReplicas("0"))
                .mappings(m -> m
                        .properties("foo", p -> p.text(tp -> tp))
                )
        )
);
PutIndexTemplateResponse response = client.indices().putIndexTemplate(pit -> pit
        .name("my_index_template")
        .indexPatterns("my-index-*")
        .composedOf("my_component_template")
        .template(t -> t
                .aliases("foo", a -> a
                        .indexRouting("bar")
                )
                .settings(s -> s.numberOfShards("1").numberOfReplicas("0"))
                .mappings(m -> m
                        .properties("foo", p -> p.text(tp -> tp))
                )
        )
);

```

---

<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 24, 2024, 4:26pm UTC](https://discuss.elastic.co/t/add-componenttemplate-with-mapping-with-new-java-client/356252/9 "2024-04-24T16:26:09Z")

</div>

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