- Java REST Client (deprecated): other versions:
- Overview
- Java Low Level REST Client
- Java High Level REST Client
- Getting started
- Document APIs
- Search APIs
- Miscellaneous APIs
- Indices APIs
- Analyze API
- Create Index API
- Delete Index API
- Indices Exists API
- Open Index API
- Close Index API
- Shrink Index API
- Split Index API
- Refresh API
- Flush API
- Flush Synced API
- Clear Cache API
- Force Merge API
- Rollover Index API
- Put Mapping API
- Get Mappings API
- Get Field Mappings API
- Index Aliases API
- Exists Alias API
- Get Alias API
- Update Indices Settings API
- Get Settings API
- Put Template API
- Validate Query API
- Get Templates API
- Get Index API
- Cluster APIs
- Ingest APIs
- Snapshot APIs
- Tasks APIs
- Script APIs
- Licensing APIs
- Machine Learning APIs
- Put Job API
- Get Job API
- Delete Job API
- Open Job API
- Close Job API
- Update Job API
- Flush Job API
- Put Datafeed API
- Get Datafeed API
- Delete Datafeed API
- Preview Datafeed API
- Start Datafeed API
- Stop Datafeed API
- Get Datafeed Stats API
- Get Job Stats API
- Forecast Job API
- Delete Forecast API
- Get Buckets API
- Get Overall Buckets API
- Get Records API
- Post Data API
- Get Influencers API
- Get Categories API
- Get Calendars API
- Put Calendar API
- Delete Calendar API
- Migration APIs
- Rollup APIs
- Security APIs
- Watcher APIs
- Graph APIs
- Using Java Builders
- Migration Guide
- License
Put Template API
editPut Template API
editPut Index Template Request
editA PutIndexTemplateRequest
specifies the name
of a template and patterns
which controls whether the template should be applied to the new index.
Settings
editThe settings of the template will be applied to the new index whose name matches the template’s patterns.
Mappings
editThe mapping of the template will be applied to the new index whose name matches the template’s patterns.
request.mapping("_doc", "{\n" + " \"_doc\": {\n" + " \"properties\": {\n" + " \"message\": {\n" + " \"type\": \"text\"\n" + " }\n" + " }\n" + " }\n" + "}", XContentType.JSON);
The mapping source can be provided in different ways in addition to the
String
example shown above:
Map<String, Object> jsonMap = new HashMap<>(); Map<String, Object> message = new HashMap<>(); message.put("type", "text"); Map<String, Object> properties = new HashMap<>(); properties.put("message", message); Map<String, Object> mapping = new HashMap<>(); mapping.put("properties", properties); jsonMap.put("_doc", mapping); request.mapping("_doc", jsonMap);
XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.startObject("_doc"); { builder.startObject("properties"); { builder.startObject("message"); { builder.field("type", "text"); } builder.endObject(); } builder.endObject(); } builder.endObject(); } builder.endObject(); request.mapping("_doc", builder);
Mapping source provided as an |
Aliases
editThe 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.
Order
editIn 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.
Version
editA template can optionally specify a version number which can be used to simplify template management by external systems.
Providing the whole source
editThe whole source including all of its sections (mappings, settings and aliases) can also be provided:
request.source("{\n" + " \"index_patterns\": [\n" + " \"log-*\",\n" + " \"pattern-1\"\n" + " ],\n" + " \"order\": 1,\n" + " \"settings\": {\n" + " \"number_of_shards\": 1\n" + " },\n" + " \"mappings\": {\n" + " \"_doc\": {\n" + " \"properties\": {\n" + " \"message\": {\n" + " \"type\": \"text\"\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"aliases\": {\n" + " \"alias-1\": {},\n" + " \"{index}-alias\": {}\n" + " }\n" + "}", XContentType.JSON);
Optional arguments
editThe following arguments can optionally be provided:
Synchronous Execution
editAcknowledgedResponse putTemplateResponse = client.indices().putTemplate(request, RequestOptions.DEFAULT);
Asynchronous Execution
editThe asynchronous execution of a put template request requires both the PutIndexTemplateRequest
instance and an ActionListener
instance to be passed to the asynchronous method:
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:
Put Index Template Response
editThe returned PutIndexTemplateResponse
allows to retrieve information about the
executed operation as follows:
On this page