Skip to content

Commit 0829889

Browse files
Spynachristophstrobl
authored andcommitted
DATAES-547 - ElasticSearchTemplate.delete(DeleteQuery, Class) does not delete documents.
Original Pull Request: spring-projects#257
1 parent 3b99aa0 commit 0829889

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.core;
17+
18+
import lombok.Value;
19+
20+
/**
21+
* DeleteEntry
22+
*
23+
* @author Lorenzo Spinelli
24+
*/
25+
@Value
26+
public class DeleteEntry {
27+
28+
private final String id;
29+
private final String indexName;
30+
31+
}

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplate.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
* @author Peter Nowak
133133
* @author Ivan Greene
134134
* @author Christoph Strobl
135+
* @author Lorenzo Spinelli
135136
*/
136137
public class ElasticsearchRestTemplate
137138
implements ElasticsearchOperations, EsClient<RestHighLevelClient>, ApplicationContextAware {
@@ -868,13 +869,14 @@ public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {
868869
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(deleteQuery.getQuery()).withIndices(indexName)
869870
.withTypes(typeName).withPageable(PageRequest.of(0, pageSize)).build();
870871

871-
SearchResultMapper onlyIdResultMapper = new SearchResultMapperAdapter() {
872+
SearchResultMapper deleteentryResultMapper = new SearchResultMapperAdapter() {
872873
@Override
873874
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
874-
List<String> result = new ArrayList<String>();
875+
List<DeleteEntry> result = new ArrayList<>();
875876
for (SearchHit searchHit : response.getHits().getHits()) {
876877
String id = searchHit.getId();
877-
result.add(id);
878+
String indexName = searchHit.getIndex();
879+
result.add(new DeleteEntry(id, indexName));
878880
}
879881
if (result.size() > 0) {
880882
return new AggregatedPageImpl<>((List<T>) result, response.getScrollId());
@@ -883,18 +885,19 @@ public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz,
883885
}
884886
};
885887

886-
Page<String> scrolledResult = startScroll(scrollTimeInMillis, searchQuery, String.class, onlyIdResultMapper);
888+
Page<DeleteEntry> scrolledResult = startScroll(scrollTimeInMillis, searchQuery, DeleteEntry.class,
889+
deleteentryResultMapper);
887890
BulkRequest request = new BulkRequest();
888-
List<String> ids = new ArrayList<String>();
891+
List<DeleteEntry> documentsToDelete = new ArrayList<>();
889892

890893
do {
891-
ids.addAll(scrolledResult.getContent());
894+
documentsToDelete.addAll(scrolledResult.getContent());
892895
scrolledResult = continueScroll(((ScrolledPage<T>) scrolledResult).getScrollId(), scrollTimeInMillis,
893-
String.class, onlyIdResultMapper);
896+
DeleteEntry.class, deleteentryResultMapper);
894897
} while (scrolledResult.getContent().size() != 0);
895898

896-
for (String id : ids) {
897-
request.add(new DeleteRequest(indexName, typeName, id));
899+
for (DeleteEntry entry : documentsToDelete) {
900+
request.add(new DeleteRequest(entry.getIndexName(), typeName, entry.getId()));
898901
}
899902

900903
if (request.numberOfActions() > 0) {

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@
128128
*/
129129
public class ElasticsearchTemplate implements ElasticsearchOperations, EsClient<Client>, ApplicationContextAware {
130130

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");
132133
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchTemplate.class);
133134
private static final String FIELD_SCORE = "_score";
134135

@@ -755,13 +756,15 @@ public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {
755756
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(deleteQuery.getQuery()).withIndices(indexName)
756757
.withTypes(typeName).withPageable(PageRequest.of(0, pageSize)).build();
757758

758-
SearchResultMapper onlyIdResultMapper = new SearchResultMapperAdapter() {
759+
SearchResultMapper deleteEntryResultMapper = new SearchResultMapperAdapter() {
759760
@Override
760761
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<>();
762763
for (SearchHit searchHit : response.getHits().getHits()) {
764+
763765
String id = searchHit.getId();
764-
result.add(id);
766+
String indexName = searchHit.getIndex();
767+
result.add(new DeleteEntry(id, indexName));
765768
}
766769
if (result.size() > 0) {
767770
return new AggregatedPageImpl<T>((List<T>) result, response.getScrollId());
@@ -770,18 +773,19 @@ public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz,
770773
}
771774
};
772775

773-
Page<String> scrolledResult = startScroll(scrollTimeInMillis, searchQuery, String.class, onlyIdResultMapper);
776+
Page<DeleteEntry> scrolledResult = startScroll(scrollTimeInMillis, searchQuery, DeleteEntry.class,
777+
deleteEntryResultMapper);
774778
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
775-
List<String> ids = new ArrayList<String>();
779+
List<DeleteEntry> documentsToDelete = new ArrayList<>();
776780

777781
do {
778-
ids.addAll(scrolledResult.getContent());
782+
documentsToDelete.addAll(scrolledResult.getContent());
779783
scrolledResult = continueScroll(((ScrolledPage<T>) scrolledResult).getScrollId(), scrollTimeInMillis,
780-
String.class, onlyIdResultMapper);
784+
DeleteEntry.class, deleteEntryResultMapper);
781785
} while (scrolledResult.getContent().size() != 0);
782786

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()));
785789
}
786790

787791
if (bulkRequestBuilder.numberOfActions() > 0) {

0 commit comments

Comments
 (0)