mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-28 14:52:20 +00:00
DATAES-19 : Delete specific type in an index, Type exists check
This commit is contained in:
parent
99a30a2a4b
commit
00ac1e8c95
@ -203,13 +203,21 @@ public interface ElasticsearchOperations {
|
||||
|
||||
/**
|
||||
* Deletes an index for given entity
|
||||
*
|
||||
*
|
||||
* @param clazz
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
<T> boolean deleteIndex(Class<T> clazz);
|
||||
|
||||
/**
|
||||
* Deletes a type in an index
|
||||
*
|
||||
* @param index
|
||||
* @param type
|
||||
*/
|
||||
void deleteType(String index, String type);
|
||||
|
||||
/**
|
||||
* check if index is exists
|
||||
*
|
||||
@ -219,6 +227,15 @@ public interface ElasticsearchOperations {
|
||||
*/
|
||||
<T> boolean indexExists(Class<T> clazz);
|
||||
|
||||
/**
|
||||
* check if type is exists in an index
|
||||
*
|
||||
* @param index
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
boolean typeExists(String index, String type);
|
||||
|
||||
/**
|
||||
* refresh the index
|
||||
*
|
||||
|
@ -19,6 +19,7 @@ import org.apache.commons.collections.CollectionUtils;
|
||||
import org.codehaus.jackson.map.DeserializationConfig;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
||||
import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest;
|
||||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
|
||||
import org.elasticsearch.action.bulk.BulkItemResponse;
|
||||
import org.elasticsearch.action.bulk.BulkRequestBuilder;
|
||||
@ -252,6 +253,12 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
return indexExists(getPersistentEntityFor(clazz).getIndexName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean typeExists(String index, String type) {
|
||||
return client.admin().cluster().prepareState().execute().actionGet()
|
||||
.getState().metaData().index(index).mappings().containsKey(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> boolean deleteIndex(Class<T> clazz) {
|
||||
String indexName = getPersistentEntityFor(clazz).getIndexName();
|
||||
@ -261,6 +268,15 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteType(String index, String type){
|
||||
Map mappings = client.admin().cluster().prepareState().execute().actionGet()
|
||||
.getState().metaData().index(index).mappings();
|
||||
if (mappings.containsKey(type)) {
|
||||
client.admin().indices().deleteMapping(new DeleteMappingRequest(index).type(type)).actionGet();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String delete(String indexName, String type, String id) {
|
||||
return client.prepareDelete(indexName, type, id).execute().actionGet().getId();
|
||||
|
@ -42,9 +42,7 @@ import java.util.List;
|
||||
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
|
||||
import static org.elasticsearch.index.query.FilterBuilders.boolFilter;
|
||||
import static org.elasticsearch.index.query.FilterBuilders.termFilter;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.fieldQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@ -829,4 +827,26 @@ public class ElasticsearchTemplateTests {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeleteSpecifiedTypeFromAnIndex() {
|
||||
// given
|
||||
String documentId = randomNumeric(5);
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setMessage("some message");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(documentId);
|
||||
indexQuery.setObject(sampleEntity);
|
||||
|
||||
elasticsearchTemplate.index(indexQuery);
|
||||
|
||||
// when
|
||||
elasticsearchTemplate.deleteType("test-index","test-type");
|
||||
|
||||
//then
|
||||
boolean typeExists = elasticsearchTemplate.typeExists("test-index", "test-type");
|
||||
assertThat(typeExists, is(false));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user