mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-05 02:02:12 +00:00
DATAES-72 - Enhance Delete Index in ElasticsearchTemplate
added deleteIndex(String indexName) and indexExist(String indexName) methods
This commit is contained in:
parent
d75dbbba94
commit
00eef9cb6c
@ -275,6 +275,14 @@ public interface ElasticsearchOperations {
|
||||
*/
|
||||
<T> boolean deleteIndex(Class<T> clazz);
|
||||
|
||||
/**
|
||||
* Deletes an index for given indexName
|
||||
*
|
||||
* @param indexName
|
||||
* @return
|
||||
*/
|
||||
boolean deleteIndex(String indexName);
|
||||
|
||||
/**
|
||||
* Deletes a type in an index
|
||||
*
|
||||
@ -292,6 +300,14 @@ public interface ElasticsearchOperations {
|
||||
*/
|
||||
<T> boolean indexExists(Class<T> clazz);
|
||||
|
||||
/**
|
||||
* check if index is exists for given IndexName
|
||||
*
|
||||
* @param indexName
|
||||
* @return
|
||||
*/
|
||||
boolean indexExists(String indexName);
|
||||
|
||||
/**
|
||||
* check if type is exists in an index
|
||||
*
|
||||
|
@ -351,6 +351,11 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
return indexExists(getPersistentEntityFor(clazz).getIndexName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean indexExists(String indexName) {
|
||||
return client.admin().indices().exists(indicesExistsRequest(indexName)).actionGet().isExists();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean typeExists(String index, String type) {
|
||||
return client.admin().cluster().prepareState().execute().actionGet()
|
||||
@ -359,7 +364,11 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
|
||||
@Override
|
||||
public <T> boolean deleteIndex(Class<T> clazz) {
|
||||
String indexName = getPersistentEntityFor(clazz).getIndexName();
|
||||
return deleteIndex(getPersistentEntityFor(clazz).getIndexName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteIndex(String indexName) {
|
||||
if (indexExists(indexName)) {
|
||||
return client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet().isAcknowledged();
|
||||
}
|
||||
@ -533,10 +542,6 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
return indexExists(getPersistentEntityFor(clazz).getIndexName()) || createIndexWithSettings(clazz);
|
||||
}
|
||||
|
||||
private boolean indexExists(String indexName) {
|
||||
return client.admin().indices().exists(indicesExistsRequest(indexName)).actionGet().isExists();
|
||||
}
|
||||
|
||||
private <T> boolean createIndexWithSettings(Class<T> clazz) {
|
||||
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||
return client.admin().indices()
|
||||
|
@ -1220,6 +1220,7 @@ public class ElasticsearchTemplateTests {
|
||||
@Test
|
||||
public void shouldReturnCountForGivenSearchQueryWithGivenMultiIndices() {
|
||||
// given
|
||||
cleanUpIndices();
|
||||
String documentId1 = randomNumeric(5);
|
||||
SampleEntity sampleEntity1 = new SampleEntityBuilder(documentId1).message("some message")
|
||||
.version(System.currentTimeMillis()).build();
|
||||
@ -1252,12 +1253,33 @@ public class ElasticsearchTemplateTests {
|
||||
assertThat(count, is(equalTo(2L)));
|
||||
}
|
||||
|
||||
private void cleanUpIndices() {
|
||||
elasticsearchTemplate.deleteIndex("test-index-1");
|
||||
elasticsearchTemplate.deleteIndex("test-index-2");
|
||||
}
|
||||
|
||||
/*
|
||||
DATAES-72
|
||||
*/
|
||||
@Test
|
||||
public void shouldDeleteIndexForSpecifiedIndexName() {
|
||||
//given
|
||||
elasticsearchTemplate.createIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
|
||||
// when
|
||||
elasticsearchTemplate.deleteIndex("test-index");
|
||||
// then
|
||||
assertThat(elasticsearchTemplate.indexExists("test-index"), is(false));
|
||||
}
|
||||
|
||||
/*
|
||||
DATAES-67
|
||||
*/
|
||||
@Test
|
||||
public void shouldReturnCountForGivenSearchQueryWithGivenIndexNameForSpecificIndex() {
|
||||
// given
|
||||
cleanUpIndices();
|
||||
String documentId1 = randomNumeric(5);
|
||||
SampleEntity sampleEntity1 = new SampleEntityBuilder(documentId1).message("some message")
|
||||
.version(System.currentTimeMillis()).build();
|
||||
|
Loading…
x
Reference in New Issue
Block a user