128
128
*/
129
129
public class ElasticsearchTemplate implements ElasticsearchOperations , EsClient <Client >, ApplicationContextAware {
130
130
131
- private static final Logger QUERY_LOGGER = LoggerFactory .getLogger ("org.springframework.data.elasticsearch.core.QUERY" );
131
+ private static final Logger QUERY_LOGGER = LoggerFactory
132
+ .getLogger ("org.springframework.data.elasticsearch.core.QUERY" );
132
133
private static final Logger LOGGER = LoggerFactory .getLogger (ElasticsearchTemplate .class );
133
134
private static final String FIELD_SCORE = "_score" ;
134
135
@@ -755,13 +756,15 @@ public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {
755
756
SearchQuery searchQuery = new NativeSearchQueryBuilder ().withQuery (deleteQuery .getQuery ()).withIndices (indexName )
756
757
.withTypes (typeName ).withPageable (PageRequest .of (0 , pageSize )).build ();
757
758
758
- SearchResultMapper onlyIdResultMapper = new SearchResultMapperAdapter () {
759
+ SearchResultMapper deleteEntryResultMapper = new SearchResultMapperAdapter () {
759
760
@ Override
760
761
public <T > AggregatedPage <T > mapResults (SearchResponse response , Class <T > clazz , Pageable pageable ) {
761
- List <String > result = new ArrayList <String >();
762
+ List <DeleteEntry > result = new ArrayList <>();
762
763
for (SearchHit searchHit : response .getHits ().getHits ()) {
764
+
763
765
String id = searchHit .getId ();
764
- result .add (id );
766
+ String indexName = searchHit .getIndex ();
767
+ result .add (new DeleteEntry (id , indexName ));
765
768
}
766
769
if (result .size () > 0 ) {
767
770
return new AggregatedPageImpl <T >((List <T >) result , response .getScrollId ());
@@ -770,18 +773,19 @@ public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz,
770
773
}
771
774
};
772
775
773
- Page <String > scrolledResult = startScroll (scrollTimeInMillis , searchQuery , String .class , onlyIdResultMapper );
776
+ Page <DeleteEntry > scrolledResult = startScroll (scrollTimeInMillis , searchQuery , DeleteEntry .class ,
777
+ deleteEntryResultMapper );
774
778
BulkRequestBuilder bulkRequestBuilder = client .prepareBulk ();
775
- List <String > ids = new ArrayList <String >();
779
+ List <DeleteEntry > documentsToDelete = new ArrayList <>();
776
780
777
781
do {
778
- ids .addAll (scrolledResult .getContent ());
782
+ documentsToDelete .addAll (scrolledResult .getContent ());
779
783
scrolledResult = continueScroll (((ScrolledPage <T >) scrolledResult ).getScrollId (), scrollTimeInMillis ,
780
- String .class , onlyIdResultMapper );
784
+ DeleteEntry .class , deleteEntryResultMapper );
781
785
} while (scrolledResult .getContent ().size () != 0 );
782
786
783
- for (String id : ids ) {
784
- bulkRequestBuilder .add (client .prepareDelete (indexName , typeName , id ));
787
+ for (DeleteEntry entry : documentsToDelete ) {
788
+ bulkRequestBuilder .add (client .prepareDelete (entry . getIndexName () , typeName , entry . getId () ));
785
789
}
786
790
787
791
if (bulkRequestBuilder .numberOfActions () > 0 ) {
0 commit comments