Problem with template loading : PutIndexTemplateRequestBuilder JAVA API


(philippe v) #1

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/499a9bc9-e00e-48c9-b7d3-3d7b8d49b86c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Binh Ly-2) #2

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/8bcaf95a-5621-466e-a7d3-3d07423bafb9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(philippe v) #3

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/86d6cedf-9642-46c7-91d4-65bf6081fc06%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Nathaniel Richand) #4

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/7681b4d4-c4ca-470c-9d7c-9f67f95a4326%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(philippe v) #5

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/1622058f-fa94-4556-ba6a-b4ec5142782c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #6