mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-29 07:12:26 +00:00
DATAES-19 : Delete specific type in an index, Type exists check
This commit is contained in:
parent
99a30a2a4b
commit
00ac1e8c95
@ -210,6 +210,14 @@ public interface ElasticsearchOperations {
|
|||||||
*/
|
*/
|
||||||
<T> boolean deleteIndex(Class<T> clazz);
|
<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
|
* check if index is exists
|
||||||
*
|
*
|
||||||
@ -219,6 +227,15 @@ public interface ElasticsearchOperations {
|
|||||||
*/
|
*/
|
||||||
<T> boolean indexExists(Class<T> clazz);
|
<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
|
* refresh the index
|
||||||
*
|
*
|
||||||
|
@ -19,6 +19,7 @@ import org.apache.commons.collections.CollectionUtils;
|
|||||||
import org.codehaus.jackson.map.DeserializationConfig;
|
import org.codehaus.jackson.map.DeserializationConfig;
|
||||||
import org.codehaus.jackson.map.ObjectMapper;
|
import org.codehaus.jackson.map.ObjectMapper;
|
||||||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
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.admin.indices.mapping.put.PutMappingRequestBuilder;
|
||||||
import org.elasticsearch.action.bulk.BulkItemResponse;
|
import org.elasticsearch.action.bulk.BulkItemResponse;
|
||||||
import org.elasticsearch.action.bulk.BulkRequestBuilder;
|
import org.elasticsearch.action.bulk.BulkRequestBuilder;
|
||||||
@ -252,6 +253,12 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
|||||||
return indexExists(getPersistentEntityFor(clazz).getIndexName());
|
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
|
@Override
|
||||||
public <T> boolean deleteIndex(Class<T> clazz) {
|
public <T> boolean deleteIndex(Class<T> clazz) {
|
||||||
String indexName = getPersistentEntityFor(clazz).getIndexName();
|
String indexName = getPersistentEntityFor(clazz).getIndexName();
|
||||||
@ -261,6 +268,15 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
|||||||
return false;
|
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
|
@Override
|
||||||
public String delete(String indexName, String type, String id) {
|
public String delete(String indexName, String type, String id) {
|
||||||
return client.prepareDelete(indexName, type, id).execute().actionGet().getId();
|
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.apache.commons.lang.RandomStringUtils.randomNumeric;
|
||||||
import static org.elasticsearch.index.query.FilterBuilders.boolFilter;
|
import static org.elasticsearch.index.query.FilterBuilders.boolFilter;
|
||||||
import static org.elasticsearch.index.query.FilterBuilders.termFilter;
|
import static org.elasticsearch.index.query.FilterBuilders.termFilter;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.fieldQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.junit.Assert.*;
|
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