# Creating IndexTemplate with Elasticsearch with Java API

**URL:** https://discuss.elastic.co/t/creating-indextemplate-with-elasticsearch-with-java-api/39169
**Category:** Elasticsearch
**Created:** [January 13, 2016, 11:40pm UTC](https://discuss.elastic.co/t/creating-indextemplate-with-elasticsearch-with-java-api/39169 "2016-01-13T23:40:07Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jama707](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jama707/32/114482_2.png) [@jama707](https://discuss.elastic.co/u/jama707)
#### Post date: [January 13, 2016, 11:40pm UTC](https://discuss.elastic.co/t/creating-indextemplate-with-elasticsearch-with-java-api/39169/1 "2016-01-13T23:40:07Z")

</div>

I'm trying to create new Template with ElasticSearch Java API. I've a JSON:

```
{ "template": "sample*",
      "mappings": {
        "someType": {
          "_all": {
            "enabled": false
          },
          "_id": {
            "path": "id"
          },
          "properties": {
            "id": {
              "type": "string",
              "index": "not_analyzed"
            },
            "name": {
              "type": "string",
              "index": "not_analyzed"
            },
            "someField":{
              ....
            }
          }
        },
        "someOtherType":{
          ...
        },
        "someOtherType2":{
          ...
        }
      }
    }

```

I wanted to create a template with a name and here is what I've done:

```
public void putTemplate(Map<String, Object> jsonTemplateAsMap) {
        PutIndexTemplateRequestBuilder builder = new PutIndexTemplateRequestBuilder(client.admin().indices(), templateName)
                .setTemplate(String.valueOf(jsonTemplateAsMap.get("template")));

        Map<String, Object> mapping = (Map<String, Object>) jsonTemplateAsMap.get("mappings");
        for (Map.Entry<String, Object> entry : mapping.entrySet()) {
            builder.addMapping(entry.getKey(), ((Map<String, Object>) entry.getValue()));
        }
        builder.get();
    }

```

1. Converting JSON String to Map.
2. Traversing the map: _key_ is document type, _value_ is its mapping.  
This code creates the template but I'm not sure if that's the standard way of creating an index template with Java API. I couldn't find any documentation on this. What is the standard way of doing this, is there any documentation? What I don't like about my current approach is the explicit conversions, which is arguably not very reliable.

---

<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: [July 5, 2017, 11:24pm UTC](https://discuss.elastic.co/t/creating-indextemplate-with-elasticsearch-with-java-api/39169/2 "2017-07-05T23:24:47Z")

</div>


