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.1Copy 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.