# Automatic index creation, templates and UnavailableShardsException

**URL:** <https://discuss.elastic.co/t/automatic-index-creation-templates-and-unavailableshardsexception/9222>\
**Category:** Elasticsearch\
**Created:** [October 3, 2012, 8:37am UTC](https://discuss.elastic.co/t/automatic-index-creation-templates-and-unavailableshardsexception/9222 "2012-10-03T08:37:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Itamar\_Syn\_Hershko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itamar_syn_hershko/32/725_2.png) [@Itamar\_Syn\_Hershko](https://discuss.elastic.co/u/Itamar_Syn_Hershko)\
**Post date:** [October 3, 2012, 8:37am UTC](https://discuss.elastic.co/t/automatic-index-creation-templates-and-unavailableshardsexception/9222/1 "2012-10-03T08:37:38Z")

</div>

Hi all,

Like all ES newbies, I also get UnavailableShardsException when using the  
Java API. I have a 3-node cluster, 2 data + the Java non-data client. The 2  
ES nodes have replicas and shards # configured in yml, but unfortunately I  
can't seem to find where to configure these settings in the Java Client API  
for the non-data node embedded in my app?

I tried bypassing this using index templates, but these settings don't seem  
to be honored. My code below.

```
final String GeneralTemplate = "{\n" +
        " \"template\" : \"el-*\",\n" +
        " \"settings\" : {\n" +
        " \"number_of_shards\" : 1,\n" +
        " \"number_of_replicas\" : 0 \n" +
        " },\n" +
        " \"mappings\" : {\n" +
        " \"foo\" : {\n" +
        // ...
        " }\n" + " }\n" +
        " }\n" +
        "}";

```

node.client().admin().indices().preparePutTemplate("general")  
.setSource(GeneralTemplate).execute().actionGet(); //  
executed _after_ waiting for green status

My last resort is to manually create indexes with a index-exists check  
beforehand, and providing those settings per index. I can verify this  
works, but I'd really like to avoid that.

Thanks in advance.

Itamar.

--

---

<div class="post-metadata">

**Author:** ![Itamar\_Syn\_Hershko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itamar_syn_hershko/32/725_2.png) [@Itamar\_Syn\_Hershko](https://discuss.elastic.co/u/Itamar_Syn_Hershko)\
**Post date:** [October 3, 2012, 1:17pm UTC](https://discuss.elastic.co/t/automatic-index-creation-templates-and-unavailableshardsexception/9222/2 "2012-10-03T13:17:23Z")

</div>

Found my issue - somehow the cluster was with a red status  
(unassigned\_shards == 51 or so) and the call to setWaitForGreenStatus went  
through without throwing any error.

Not sure how it got to that state or why the ES didn't error. After  
starting over with a fresh cluster everything is working properly.

On Wed, Oct 3, 2012 at 10:37 AM, Itamar Syn-Hershko [itamar@code972.com](mailto:itamar@code972.com)wrote:

> Hi all,
> 
> Like all ES newbies, I also get UnavailableShardsException when using the  
> Java API. I have a 3-node cluster, 2 data + the Java non-data client. The 2  
> ES nodes have replicas and shards # configured in yml, but unfortunately I  
> can't seem to find where to configure these settings in the Java Client API  
> for the non-data node embedded in my app?
> 
> I tried bypassing this using index templates, but these settings don't  
> seem to be honored. My code below.
> 
> ```
> final String GeneralTemplate = "{\n" +
> " \"template\" : \"el-*\",\n" +
> " \"settings\" : {\n" +
> " \"number_of_shards\" : 1,\n" +
> " \"number_of_replicas\" : 0 \n" +
> " },\n" +
> " \"mappings\" : {\n" +
> " \"foo\" : {\n" +
> // ...
> " }\n" + " }\n" +
> " }\n" +
> "}";
> 
> ```
> 
> node.client().admin().indices().preparePutTemplate("general")  
> .setSource(GeneralTemplate).execute().actionGet(); //  
> executed _after_ waiting for green status
> 
> My last resort is to manually create indexes with a index-exists check  
> beforehand, and providing those settings per index. I can verify this  
> works, but I'd really like to avoid that.
> 
> Thanks in advance.
> 
> Itamar.

--

---

<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 6, 2017, 3:10am UTC](https://discuss.elastic.co/t/automatic-index-creation-templates-and-unavailableshardsexception/9222/3 "2017-07-06T03:10:24Z")

</div>

Closed on July 6, 2017, 3:10am UTC


