[[java-rest-high-put-template]]
=== Put Template API

[[java-rest-high-put-template-request]]
==== Put Index Template Request

A `PutIndexTemplateRequest` specifies the `name` of a template and `patterns`
which controls whether the template should be applied to the new index.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request]
--------------------------------------------------
<1> The name of the template
<2> The patterns of the template

==== Settings
The settings of the template will be applied to the new index whose name matches the
template's patterns.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-settings]
--------------------------------------------------
<1> Settings for this template

[[java-rest-high-put-template-request-mappings]]
==== Mappings
The mapping of the template will be applied to the new index whose name matches the
template's patterns.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-mappings-json]
--------------------------------------------------
<1> The type to define
<2> The mapping for this type, provided as a JSON string

The mapping source can be provided in different ways in addition to the
`String` example shown above:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-mappings-map]
--------------------------------------------------
<1> Mapping source provided as a `Map` which gets automatically converted
to JSON format

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-mappings-xcontent]
--------------------------------------------------
<1> Mapping source provided as an `XContentBuilder` object, the Elasticsearch
built-in helpers to generate JSON content

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-mappings-shortcut]
--------------------------------------------------
<1> Mapping source provided as `Object` key-pairs, which gets converted to
JSON format

==== Aliases
The aliases of the template will define aliasing to the index whose name matches the
template's patterns. A placeholder `{index}` can be used in an alias of a template.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-aliases]
--------------------------------------------------
<1> The alias to define
<2> The alias to define with placeholder

==== Order
In case multiple templates match an index, the orders of matching templates determine
the sequence that settings, mappings, and alias of each matching template is applied.
Templates with lower orders are applied first, and higher orders override them.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-order]
--------------------------------------------------
<1> The order of the template

==== Version
A template can optionally specify a version number which can be used to simplify template
management by external systems.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-version]
--------------------------------------------------
<1> The version number of the template

==== Providing the whole source
The whole source including all of its sections (mappings, settings and aliases)
can also be provided:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-whole-source]
--------------------------------------------------
<1> The source provided as a JSON string. It can also be provided as a `Map`
or an `XContentBuilder`.

==== Optional arguments
The following arguments can optionally be provided:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-create]
--------------------------------------------------
<1> To force to only create a new template; do not overwrite the existing template

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-masterTimeout]
--------------------------------------------------
<1> Timeout to connect to the master node as a `TimeValue`
<2> Timeout to connect to the master node as a `String`

[[java-rest-high-put-template-sync]]
==== Synchronous Execution

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-execute]
--------------------------------------------------

[[java-rest-high-put-template-async]]
==== Asynchronous Execution

The asynchronous execution of a put template request requires both the `PutIndexTemplateRequest`
instance and an `ActionListener` instance to be passed to the asynchronous method:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-execute-async]
--------------------------------------------------
<1> The `PutIndexTemplateRequest` to execute and the `ActionListener` to use when
the execution completes

The asynchronous method does not block and returns immediately. Once it is
completed the `ActionListener` is called back using the `onResponse` method
if the execution successfully completed or using the `onFailure` method if
it failed.

A typical listener for `PutIndexTemplateResponse` looks like:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-execute-listener]
--------------------------------------------------
<1> Called when the execution is successfully completed. The response is
provided as an argument
<2> Called in case of failure. The raised exception is provided as an argument

[[java-rest-high-put-template-response]]
==== Put Index Template Response

The returned `PutIndexTemplateResponse` allows to retrieve information about the
executed operation as follows:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-response]
--------------------------------------------------
<1> Indicates whether all of the nodes have acknowledged the request
