DATAES-19 : Delete specific type in an index, Type exists check

This commit is contained in:
Mohsin Husen
2013-08-09 12:57:02 +01:00
parent 99a30a2a4b
commit 00ac1e8c95
3 changed files with 57 additions and 4 deletions

View File

@@ -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
*

View File

@@ -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();