mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-23 04:22:12 +00:00
Added support for Delete index and IndexExists methods to Template
This commit is contained in:
parent
aea8dd9d09
commit
26cd1ec4b8
@ -197,6 +197,22 @@ public interface ElasticsearchOperations {
|
|||||||
*/
|
*/
|
||||||
<T> void delete(DeleteQuery query, Class<T> clazz);
|
<T> void delete(DeleteQuery query, Class<T> clazz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes an index for given entity
|
||||||
|
* @param clazz
|
||||||
|
* @param <T>
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
<T> boolean deleteIndex(Class<T> clazz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if index is exists
|
||||||
|
* @param clazz
|
||||||
|
* @param <T>
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
<T> boolean indexExists(Class<T> clazz);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* refresh the index
|
* refresh the index
|
||||||
* @param indexName
|
* @param indexName
|
||||||
|
@ -18,6 +18,7 @@ package org.springframework.data.elasticsearch.core;
|
|||||||
|
|
||||||
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.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;
|
||||||
@ -226,6 +227,20 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> boolean indexExists(Class<T> clazz){
|
||||||
|
return indexExists(getPersistentEntityFor(clazz).getIndexName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> boolean deleteIndex(Class<T> clazz){
|
||||||
|
String indexName = getPersistentEntityFor(clazz).getIndexName();
|
||||||
|
if(indexExists(indexName)){
|
||||||
|
return client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet().acknowledged();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@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)
|
return client.prepareDelete(indexName, type, id)
|
||||||
|
@ -728,4 +728,16 @@ public class ElasticsearchTemplateTest {
|
|||||||
assertThat(elasticsearchTemplate.putMapping(entity) , is(true)) ;
|
assertThat(elasticsearchTemplate.putMapping(entity) , is(true)) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldDeleteIndexForGivenEntity(){
|
||||||
|
//given
|
||||||
|
Class clazz = SampleEntity.class;
|
||||||
|
//when
|
||||||
|
elasticsearchTemplate.deleteIndex(clazz);
|
||||||
|
//then
|
||||||
|
assertThat(elasticsearchTemplate.indexExists(clazz),is(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user