mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-05 18:22:23 +00:00
DATAES-547 - ElasticSearchTemplate.delete(DeleteQuery, Class) does not delete documents.
Original Pull Request: #257
This commit is contained in:
parent
5f3f3ee31d
commit
5bfcb4214c
@ -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;
|
||||
}
|
||||
}
|
@ -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 <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());
|
||||
}
|
||||
return new AggregatedPageImpl<T>(Collections.EMPTY_LIST, response.getScrollId());
|
||||
return new AggregatedPageImpl<T>(Collections.emptyList(), response.getScrollId());
|
||||
}
|
||||
};
|
||||
|
||||
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());
|
||||
scrolledResult = continueScroll(((ScrolledPage<T>)scrolledResult).getScrollId(), scrollTimeInMillis, String.class, onlyIdResultMapper);
|
||||
} while(scrolledResult.getContent().size() != 0);
|
||||
documentsToDelete.addAll(scrolledResult.getContent());
|
||||
scrolledResult = continueScroll(((ScrolledPage<T>) 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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user