diff --git a/src/main/java/org/springframework/data/elasticsearch/core/DeleteEntry.java b/src/main/java/org/springframework/data/elasticsearch/core/DeleteEntry.java new file mode 100644 index 000000000..9cfd0d99e --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/DeleteEntry.java @@ -0,0 +1,41 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core; + +/** + * DeleteEntry + * + * @author Lorenzo Spinelli + */ +public class DeleteEntry { + + private final String id; + private final String indexName; + + public DeleteEntry(String id, String indexName) { + + this.id = id; + this.indexName = indexName; + } + + public String getId() { + return id; + } + + public String getIndexName() { + return indexName; + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java index 8a2cf52c5..bb9db50c1 100755 --- a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java @@ -701,32 +701,36 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(deleteQuery.getQuery()).withIndices(indexName) .withTypes(typeName).withPageable(PageRequest.of(0, pageSize)).build(); - SearchResultMapper onlyIdResultMapper = new SearchResultMapper() { + SearchResultMapper deleteEntryResultMapper = new SearchResultMapper() { @Override public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { - List result = new ArrayList(); + List result = new ArrayList<>(); for (SearchHit searchHit : response.getHits().getHits()) { + String id = searchHit.getId(); - result.add(id); + String indexName = searchHit.getIndex(); + result.add(new DeleteEntry(id, indexName)); } if (result.size() > 0) { return new AggregatedPageImpl((List) result, response.getScrollId()); } - return new AggregatedPageImpl(Collections.EMPTY_LIST, response.getScrollId()); + return new AggregatedPageImpl(Collections.emptyList(), response.getScrollId()); } }; - Page scrolledResult = startScroll(scrollTimeInMillis, searchQuery, String.class, onlyIdResultMapper); + Page scrolledResult = startScroll(scrollTimeInMillis, searchQuery, DeleteEntry.class, + deleteEntryResultMapper); BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); - List ids = new ArrayList(); + List documentsToDelete = new ArrayList<>(); do { - ids.addAll(scrolledResult.getContent()); - scrolledResult = continueScroll(((ScrolledPage)scrolledResult).getScrollId(), scrollTimeInMillis, String.class, onlyIdResultMapper); - } while(scrolledResult.getContent().size() != 0); + documentsToDelete.addAll(scrolledResult.getContent()); + scrolledResult = continueScroll(((ScrolledPage) scrolledResult).getScrollId(), scrollTimeInMillis, + DeleteEntry.class, deleteEntryResultMapper); + } while (scrolledResult.getContent().size() != 0); - for (String id : ids) { - bulkRequestBuilder.add(client.prepareDelete(indexName, typeName, id)); + for (DeleteEntry entry : documentsToDelete) { + bulkRequestBuilder.add(client.prepareDelete(entry.getIndexName(), typeName, entry.getId())); } if (bulkRequestBuilder.numberOfActions() > 0) {