DATAES-547 - ElasticSearchTemplate.delete(DeleteQuery, Class) does not delete documents.

Original Pull Request: #257
This commit is contained in:
Spyna 2019-03-30 19:45:14 +01:00 committed by Christoph Strobl
parent 3b99aa04ef
commit 082988955b
3 changed files with 57 additions and 19 deletions

View File

@ -0,0 +1,31 @@
/*
* 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;
import lombok.Value;
/**
* DeleteEntry
*
* @author Lorenzo Spinelli
*/
@Value
public class DeleteEntry {
private final String id;
private final String indexName;
}

View File

@ -132,6 +132,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* @author Peter Nowak
* @author Ivan Greene
* @author Christoph Strobl
* @author Lorenzo Spinelli
*/
public class ElasticsearchRestTemplate
implements ElasticsearchOperations, EsClient<RestHighLevelClient>, ApplicationContextAware {
@ -868,13 +869,14 @@ public class ElasticsearchRestTemplate
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(deleteQuery.getQuery()).withIndices(indexName)
.withTypes(typeName).withPageable(PageRequest.of(0, pageSize)).build();
SearchResultMapper onlyIdResultMapper = new SearchResultMapperAdapter() {
SearchResultMapper deleteentryResultMapper = new SearchResultMapperAdapter() {
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
List<String> result = new ArrayList<String>();
List<DeleteEntry> 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<T>) result, response.getScrollId());
@ -883,18 +885,19 @@ public class ElasticsearchRestTemplate
}
};
Page<String> scrolledResult = startScroll(scrollTimeInMillis, searchQuery, String.class, onlyIdResultMapper);
Page<DeleteEntry> scrolledResult = startScroll(scrollTimeInMillis, searchQuery, DeleteEntry.class,
deleteentryResultMapper);
BulkRequest request = new BulkRequest();
List<String> ids = new ArrayList<String>();
List<DeleteEntry> documentsToDelete = new ArrayList<>();
do {
ids.addAll(scrolledResult.getContent());
documentsToDelete.addAll(scrolledResult.getContent());
scrolledResult = continueScroll(((ScrolledPage<T>) scrolledResult).getScrollId(), scrollTimeInMillis,
String.class, onlyIdResultMapper);
DeleteEntry.class, deleteentryResultMapper);
} while (scrolledResult.getContent().size() != 0);
for (String id : ids) {
request.add(new DeleteRequest(indexName, typeName, id));
for (DeleteEntry entry : documentsToDelete) {
request.add(new DeleteRequest(entry.getIndexName(), typeName, entry.getId()));
}
if (request.numberOfActions() > 0) {

View File

@ -128,7 +128,8 @@ import org.springframework.util.StringUtils;
*/
public class ElasticsearchTemplate implements ElasticsearchOperations, EsClient<Client>, ApplicationContextAware {
private static final Logger QUERY_LOGGER = LoggerFactory.getLogger("org.springframework.data.elasticsearch.core.QUERY");
private static final Logger QUERY_LOGGER = LoggerFactory
.getLogger("org.springframework.data.elasticsearch.core.QUERY");
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchTemplate.class);
private static final String FIELD_SCORE = "_score";
@ -755,13 +756,15 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, EsClient<
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(deleteQuery.getQuery()).withIndices(indexName)
.withTypes(typeName).withPageable(PageRequest.of(0, pageSize)).build();
SearchResultMapper onlyIdResultMapper = new SearchResultMapperAdapter() {
SearchResultMapper deleteEntryResultMapper = new SearchResultMapperAdapter() {
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
List<String> result = new ArrayList<String>();
List<DeleteEntry> 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<T>((List<T>) result, response.getScrollId());
@ -770,18 +773,19 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, EsClient<
}
};
Page<String> scrolledResult = startScroll(scrollTimeInMillis, searchQuery, String.class, onlyIdResultMapper);
Page<DeleteEntry> scrolledResult = startScroll(scrollTimeInMillis, searchQuery, DeleteEntry.class,
deleteEntryResultMapper);
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
List<String> ids = new ArrayList<String>();
List<DeleteEntry> documentsToDelete = new ArrayList<>();
do {
ids.addAll(scrolledResult.getContent());
documentsToDelete.addAll(scrolledResult.getContent());
scrolledResult = continueScroll(((ScrolledPage<T>) scrolledResult).getScrollId(), scrollTimeInMillis,
String.class, onlyIdResultMapper);
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) {