Skip to content

Commit 6a99d35

Browse files
committed
DATAES-71 - Enhance Create Index in ElasticsearchTemplate
added createIndex(String indexName) method
1 parent 00eef9c commit 6a99d35

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public interface ElasticsearchOperations {
4545
*/
4646
<T> boolean createIndex(Class<T> clazz);
4747

48+
/**
49+
* Create an index for given indexName
50+
*
51+
* @param indexName
52+
*/
53+
boolean createIndex(String indexName);
54+
55+
4856
/**
4957
* Create mapping for a class
5058
*

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ public <T> boolean createIndex(Class<T> clazz) {
117117
return createIndexIfNotCreated(clazz);
118118
}
119119

120+
@Override
121+
public boolean createIndex(String indexName) {
122+
Assert.notNull(indexName, "No index defined for Query");
123+
return client.admin().indices()
124+
.create(Requests.createIndexRequest(indexName))
125+
.actionGet().isAcknowledged();
126+
}
127+
120128
@Override
121129
public <T> boolean putMapping(Class<T> clazz) {
122130
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,14 +1256,31 @@ public void shouldReturnCountForGivenSearchQueryWithGivenMultiIndices() {
12561256
private void cleanUpIndices() {
12571257
elasticsearchTemplate.deleteIndex("test-index-1");
12581258
elasticsearchTemplate.deleteIndex("test-index-2");
1259+
elasticsearchTemplate.createIndex("test-index-1");
1260+
elasticsearchTemplate.createIndex("test-index-2");
1261+
elasticsearchTemplate.refresh("test-index-1", true);
1262+
elasticsearchTemplate.refresh("test-index-2", true);
1263+
}
1264+
1265+
/*
1266+
DATAES-71
1267+
*/
1268+
@Test
1269+
public void shouldCreatedIndexWithSpecifiedIndexName() {
1270+
// given
1271+
elasticsearchTemplate.deleteIndex("test-index");
1272+
// when
1273+
elasticsearchTemplate.createIndex("test-index");
1274+
// then
1275+
assertThat(elasticsearchTemplate.indexExists("test-index"), is(true));
12591276
}
12601277

12611278
/*
12621279
DATAES-72
12631280
*/
12641281
@Test
12651282
public void shouldDeleteIndexForSpecifiedIndexName() {
1266-
//given
1283+
// given
12671284
elasticsearchTemplate.createIndex(SampleEntity.class);
12681285
elasticsearchTemplate.refresh(SampleEntity.class, true);
12691286

0 commit comments

Comments
 (0)