# Problem with template loading : PutIndexTemplateRequestBuilder JAVA API

**URL:** <https://discuss.elastic.co/t/problem-with-template-loading-putindextemplaterequestbuilder-java-api/16295>\
**Category:** Elasticsearch\
**Created:** [March 11, 2014, 3:48pm UTC](https://discuss.elastic.co/t/problem-with-template-loading-putindextemplaterequestbuilder-java-api/16295 "2014-03-11T15:48:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![philippe\_v](https://avatars.discourse-cdn.com/v4/letter/p/6de8d8/32.png) [@philippe\_v](https://discuss.elastic.co/u/philippe_v)\
**Post date:** [March 11, 2014, 3:48pm UTC](https://discuss.elastic.co/t/problem-with-template-loading-putindextemplaterequestbuilder-java-api/16295/1 "2014-03-11T15:48:29Z")

</div>

Hello guys,

I'm experiencing an issue with the PutIndexTemplateRequestBuilder.

My elasticsearch is embeded in a webapp and I need to load mappings and  
conf on the fly. So I use the PutIndexTemplateRequestBuilder.  
Sometimes it works, sometimes it doesn't. It seems that the template is not  
always loaded... Maybe I'm doing wrong.

In order to explain the problem here is a small code I wrote, just launch  
it several times changing the number of shards  
(nb : you need to run elasticsearch from command line before launching the  
app).

You'll notice that sometimes the numberOfShards log is not ok.

_Create a small maven app :_

mvn archetype:create -DgroupId=fr.carboatmedia -DartifactId=estemplate

\*Add the elasticsearch dependancy to the pom xml : \*

 org.elasticsearch elasticsearch 1.0.1

_Copy that code to the App class :_

import java.util.concurrent.ExecutionException;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;  
import  
org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequestBuilder;  
import  
org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;  
import  
org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder;  
import  
org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse;  
import org.elasticsearch.client.Client;  
import org.elasticsearch.client.Requests;  
import org.elasticsearch.client.transport.TransportClient;  
import org.elasticsearch.common.transport.InetSocketTransportAddress;  
import org.elasticsearch.common.unit.TimeValue;

public class App {

```
private static final String INDEX_NAME = "testindex";

private static final int NUMBER_OF_SHARD = 1;

/**
 * before launching, elasticsearch 1.0.1 must be running from command 

```

line  
\*  
\*/  
public static void main(String[] args) throws InterruptedException,  
ExecutionException {

```
    // Transport client
    Client client = new TransportClient().addTransportAddress(new 

```

InetSocketTransportAddress("localhost", 9300));

```
    // Creating index

```

client.admin().indices().create(Requests.createIndexRequest(INDEX\_NAME));

```
    // Creating template request (settings set to 1 shard 1 replica)
    PutIndexTemplateRequestBuilder indexTemplateRequestBuilder = 

```

client.admin().indices().preparePutTemplate(INDEX\_NAME);  
String template = "{"template" : "testindex", "settings" : {  
"index" : {"number\_of\_shards" : " + NUMBER\_OF\_SHARD + "}}}";

```
    // Executing the tempalte request
    PutIndexTemplateResponse putIndexTemplateResponse = 

```

indexTemplateRequestBuilder.setSource(template).get(TimeValue.timeValueMinutes(1));  
if (putIndexTemplateResponse.isAcknowledged()) {  
System.out.println("Mapping file successfully imported !");  
} else {  
System.err.println("Mapping file was NOT imported !");  
}

```
    // Check conf
    GetSettingsRequestBuilder getSettingsRequestBuilder = 

```

client.admin().indices().prepareGetSettings(INDEX\_NAME);  
GetSettingsResponse getSettingsResponse =  
getSettingsRequestBuilder.get(TimeValue.timeValueMinutes(1));  
String numberOfShards = getSettingsResponse.getSetting(INDEX\_NAME,  
"index.number\_of\_shards");  
System.out.println("numberOfShards " + numberOfShards);

```
    // Delete index
    DeleteIndexResponse deleteIndexResponse = 

```

client.admin().indices().delete(Requests.deleteIndexRequest(INDEX\_NAME)).get();  
if (deleteIndexResponse.isAcknowledged()) {  
System.out.println("Index successfully erased !");  
} else {  
System.err.println("Index was NOT erased !");  
}

```
}

```

}

thanks!

Philippe

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/499a9bc9-e00e-48c9-b7d3-3d7b8d49b86c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/499a9bc9-e00e-48c9-b7d3-3d7b8d49b86c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 11, 2014, 6:52pm UTC](https://discuss.elastic.co/t/problem-with-template-loading-putindextemplaterequestbuilder-java-api/16295/2 "2014-03-11T18:52:43Z")

</div>

I have a question:

Is your intention to create an index template with a specified number of  
shards, and then create an index that matches the template, and then  
retrieve the index settings, and expect it to be the same as specified in  
the template?

If the above is the case, then you probably want to execute  
indices().preparePutTemplate() first before executing indices().create()?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8bcaf95a-5621-466e-a7d3-3d07423bafb9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8bcaf95a-5621-466e-a7d3-3d07423bafb9%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![philippe\_v](https://avatars.discourse-cdn.com/v4/letter/p/6de8d8/32.png) [@philippe\_v](https://discuss.elastic.co/u/philippe_v)\
**Post date:** [March 12, 2014, 8:17am UTC](https://discuss.elastic.co/t/problem-with-template-loading-putindextemplaterequestbuilder-java-api/16295/3 "2014-03-12T08:17:50Z")

</div>

Thanks for your answer,

Yes that's my intention (in my real case my template includes mappings,  
etc...)

I tried to invert indices().preparePutTemplate() with indices().create(),  
as you mentioned, but it didn't change anything.

For exemple,

1st launch, NUMBER OF SHARD = 1  
-\> log numberOfShards 1  
2nd launch NUMBER OF SHARD = 5  
-\> log numberOfShards 1  
3rd launch NUMBER OF SHARD = 5  
-\> log numberOfShards 5

The template is taken in account only the second time.

Le mardi 11 mars 2014 19:52:43 UTC+1, Binh Ly a écrit :

> I have a question:
> 
> Is your intention to create an index template with a specified number of  
> shards, and then create an index that matches the template, and then  
> retrieve the index settings, and expect it to be the same as specified in  
> the template?
> 
> If the above is the case, then you probably want to execute  
> indices().preparePutTemplate() first before executing indices().create()?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/86d6cedf-9642-46c7-91d4-65bf6081fc06%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/86d6cedf-9642-46c7-91d4-65bf6081fc06%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Richand](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathaniel_richand/32/1724_2.png) [@Nathaniel\_Richand](https://discuss.elastic.co/u/Nathaniel_Richand)\
**Post date:** [March 17, 2014, 1:21pm UTC](https://discuss.elastic.co/t/problem-with-template-loading-putindextemplaterequestbuilder-java-api/16295/4 "2014-03-17T13:21:54Z")

</div>

Hi Philippe,

You should switch index create and template loading as Binh ly mention  
previously. You must also change the index creation to wait the response  
like this :

```
    PutIndexTemplateResponse putIndexTemplateResponse = 

```

client.admin().indices().preparePutTemplate(INDEX\_NAME).setSource(template).get(TimeValue.timeValueMinutes(1));

client.admin().indices().create(Requests.createIndexRequest(INDEX\_NAME)).get();

Le mercredi 12 mars 2014 09:17:50 UTC+1, philippe v a écrit :

> Thanks for your answer,
> 
> Yes that's my intention (in my real case my template includes mappings,  
> etc...)
> 
> I tried to invert indices().preparePutTemplate() with indices().create(),  
> as you mentioned, but it didn't change anything.
> 
> For exemple,
> 
> 1st launch, NUMBER OF SHARD = 1  
> -\> log numberOfShards 1  
> 2nd launch NUMBER OF SHARD = 5  
> -\> log numberOfShards 1  
> 3rd launch NUMBER OF SHARD = 5  
> -\> log numberOfShards 5
> 
> The template is taken in account only the second time.
> 
> Le mardi 11 mars 2014 19:52:43 UTC+1, Binh Ly a écrit :
> 
> > I have a question:
> > 
> > Is your intention to create an index template with a specified number of  
> > shards, and then create an index that matches the template, and then  
> > retrieve the index settings, and expect it to be the same as specified in  
> > the template?
> > 
> > If the above is the case, then you probably want to execute  
> > indices().preparePutTemplate() first before executing indices().create()?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7681b4d4-c4ca-470c-9d7c-9f67f95a4326%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7681b4d4-c4ca-470c-9d7c-9f67f95a4326%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![philippe\_v](https://avatars.discourse-cdn.com/v4/letter/p/6de8d8/32.png) [@philippe\_v](https://discuss.elastic.co/u/philippe_v)\
**Post date:** [March 17, 2014, 1:29pm UTC](https://discuss.elastic.co/t/problem-with-template-loading-putindextemplaterequestbuilder-java-api/16295/5 "2014-03-17T13:29:11Z")

</div>

Yep,

It worked, thanks a lot.

Le lundi 17 mars 2014 14:21:54 UTC+1, Nathaniel Richand a écrit :

> Hi Philippe,
> 
> You should switch index create and template loading as Binh ly mention  
> previously. You must also change the index creation to wait the response  
> like this :
> 
> ```
> PutIndexTemplateResponse putIndexTemplateResponse = 
> 
> ```
> 
> client.admin().indices().preparePutTemplate(INDEX\_NAME).setSource(template).get(TimeValue.timeValueMinutes(1));
> 
> client.admin().indices().create(Requests.createIndexRequest(INDEX\_NAME)).get();
> 
> Le mercredi 12 mars 2014 09:17:50 UTC+1, philippe v a écrit :
> 
> > Thanks for your answer,
> > 
> > Yes that's my intention (in my real case my template includes mappings,  
> > etc...)
> > 
> > I tried to invert indices().preparePutTemplate() with indices().create(),  
> > as you mentioned, but it didn't change anything.
> > 
> > For exemple,
> > 
> > 1st launch, NUMBER OF SHARD = 1  
> > -\> log numberOfShards 1  
> > 2nd launch NUMBER OF SHARD = 5  
> > -\> log numberOfShards 1  
> > 3rd launch NUMBER OF SHARD = 5  
> > -\> log numberOfShards 5
> > 
> > The template is taken in account only the second time.
> > 
> > Le mardi 11 mars 2014 19:52:43 UTC+1, Binh Ly a écrit :
> > 
> > > I have a question:
> > > 
> > > Is your intention to create an index template with a specified number of  
> > > shards, and then create an index that matches the template, and then  
> > > retrieve the index settings, and expect it to be the same as specified in  
> > > the template?
> > > 
> > > If the above is the case, then you probably want to execute  
> > > indices().preparePutTemplate() first before executing indices().create()?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1622058f-fa94-4556-ba6a-b4ec5142782c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1622058f-fa94-4556-ba6a-b4ec5142782c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:42am UTC](https://discuss.elastic.co/t/problem-with-template-loading-putindextemplaterequestbuilder-java-api/16295/6 "2017-07-06T01:42:48Z")

</div>


