# Failure in putting template mapping using Java Api

**URL:** <https://discuss.elastic.co/t/failure-in-putting-template-mapping-using-java-api/51122>\
**Category:** Elasticsearch\
**Created:** [May 27, 2016, 7:47am UTC](https://discuss.elastic.co/t/failure-in-putting-template-mapping-using-java-api/51122 "2016-05-27T07:47:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![supernovae](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@supernovae](https://discuss.elastic.co/u/supernovae)\
**Post date:** [May 27, 2016, 7:47am UTC](https://discuss.elastic.co/t/failure-in-putting-template-mapping-using-java-api/51122/1 "2016-05-27T07:47:46Z")

</div>

Hi,  
I'm trying to put a template mapping into elasticsearch using the Java api. This is the code I'm using:

```
Client client = TransportClient.builder()
    .build()
    .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(ES_ADDRESS, 9300)));
String indexSettings;
InputStream jsonSettingsIS = Thread.currentThread().getContextClassLoader().getResourceAsStream("" + index + ".json");
indexSettings = new Scanner(jsonSettingsIS).useDelimiter("\\A").next();
try
{
   client.admin()
        .indices()
        .preparePutTemplate(index)
        .setTemplate(indexSettings)
        .get();
}
catch (IndexTemplateAlreadyExistsException e)
{
   LOGGER.info("Template index [" + index + "] already exists.");
}

```

When executing that snipped of code I get this error:

```
Exception in thread "main" InvalidIndexTemplateException[index_template [template-*] invalid, cause [Validation Failed: 1: template must not contain a space;2: template must not contain a ',';3: template must not container the following characters [\, /, *, ?, ", <, >, |, , ,];]]
at org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService.validate(MetaDataIndexTemplateService.java:218)

```

The template I'm trying to put is

```
{
    "template": "template-*",
    "settings": {
       "index": {
          "analysis": {
             "analyzer": {
                "whitespace": {
                   "type": "whitespace"
                   }
               }
            }
         }
      }
}
```

---

<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:** [May 27, 2016, 8:56am UTC](https://discuss.elastic.co/t/failure-in-putting-template-mapping-using-java-api/51122/2 "2016-05-27T08:56:07Z")

</div>

Please format better your code. This is ugly to read IMO. `</>` icon is your friend! 🙂

You need to use `setSource` instead of `setTemplate`. `setTemplate` is used to set the template name only.

---

<div class="post-metadata">

**Author:** ![supernovae](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@supernovae](https://discuss.elastic.co/u/supernovae)\
**Post date:** [May 27, 2016, 9:21am UTC](https://discuss.elastic.co/t/failure-in-putting-template-mapping-using-java-api/51122/3 "2016-05-27T09:21:05Z")

</div>

Hi, thanks for your answers. Using `setSource` actually works.  
Thanks again!

---

<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, 10:48pm UTC](https://discuss.elastic.co/t/failure-in-putting-template-mapping-using-java-api/51122/4 "2017-07-05T22:48:32Z")

</div>


