Add componentTemplate with mapping with new Java client

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

Something like:

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

Thanks for fast response.

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

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

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

This should do it.

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" +
                        "  }"))
        )
);

Is there any docs page for new client?
Like you had in v.7: java-rest-high-put-index-template-v2

Sadly no.

But you have some advices from the doc: API conventions | Elasticsearch Java API Client [8.13] | Elastic

And my blog post: Switching from the Java High Level Rest Client to the new Java API Client | Elastic Blog

And my "demo" repo:

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

Like: put-index-template

Should be straightforward:

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

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