DATAES-71 - Enhance Create Index in ElasticsearchTemplate

added createIndex(String indexName) method
This commit is contained in:
Mohsin Husen 2014-03-19 21:49:44 +00:00
parent 00eef9cb6c
commit 6a99d355c0
3 changed files with 34 additions and 1 deletions

View File

@ -45,6 +45,14 @@ public interface ElasticsearchOperations {
*/
<T> boolean createIndex(Class<T> clazz);
/**
* Create an index for given indexName
*
* @param indexName
*/
boolean createIndex(String indexName);
/**
* Create mapping for a class
*

View File

@ -117,6 +117,14 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
return createIndexIfNotCreated(clazz);
}
@Override
public boolean createIndex(String indexName) {
Assert.notNull(indexName, "No index defined for Query");
return client.admin().indices()
.create(Requests.createIndexRequest(indexName))
.actionGet().isAcknowledged();
}
@Override
public <T> boolean putMapping(Class<T> clazz) {
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);

View File

@ -1256,6 +1256,23 @@ public class ElasticsearchTemplateTests {
private void cleanUpIndices() {
elasticsearchTemplate.deleteIndex("test-index-1");
elasticsearchTemplate.deleteIndex("test-index-2");
elasticsearchTemplate.createIndex("test-index-1");
elasticsearchTemplate.createIndex("test-index-2");
elasticsearchTemplate.refresh("test-index-1", true);
elasticsearchTemplate.refresh("test-index-2", true);
}
/*
DATAES-71
*/
@Test
public void shouldCreatedIndexWithSpecifiedIndexName() {
// given
elasticsearchTemplate.deleteIndex("test-index");
// when
elasticsearchTemplate.createIndex("test-index");
// then
assertThat(elasticsearchTemplate.indexExists("test-index"), is(true));
}
/*
@ -1263,7 +1280,7 @@ public class ElasticsearchTemplateTests {
*/
@Test
public void shouldDeleteIndexForSpecifiedIndexName() {
//given
// given
elasticsearchTemplate.createIndex(SampleEntity.class);
elasticsearchTemplate.refresh(SampleEntity.class, true);