mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-22 12:02:10 +00:00
DATAES-547 - Polishing.
Add test and directly use SearchHit to pass on the index name. Fix minor flaw in Exception translation for non existing indices along the way. Original Pull Request: #257
This commit is contained in:
parent
5bfcb4214c
commit
179c3079bd
@ -1,41 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
@ -24,7 +24,7 @@ import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
@ -704,33 +704,23 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
||||
SearchResultMapper deleteEntryResultMapper = new SearchResultMapper() {
|
||||
@Override
|
||||
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||
List<DeleteEntry> result = new ArrayList<>();
|
||||
for (SearchHit searchHit : response.getHits().getHits()) {
|
||||
|
||||
String id = searchHit.getId();
|
||||
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.emptyList(), response.getScrollId());
|
||||
return new AggregatedPageImpl<>((List<T>) Arrays.asList(response.getHits().getHits()), response.getScrollId());
|
||||
}
|
||||
};
|
||||
|
||||
Page<DeleteEntry> scrolledResult = startScroll(scrollTimeInMillis, searchQuery, DeleteEntry.class,
|
||||
Page<SearchHit> scrolledResult = startScroll(scrollTimeInMillis, searchQuery, SearchHit.class,
|
||||
deleteEntryResultMapper);
|
||||
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
|
||||
List<DeleteEntry> documentsToDelete = new ArrayList<>();
|
||||
List<SearchHit> documentsToDelete = new ArrayList<>();
|
||||
|
||||
do {
|
||||
documentsToDelete.addAll(scrolledResult.getContent());
|
||||
scrolledResult = continueScroll(((ScrolledPage<T>) scrolledResult).getScrollId(), scrollTimeInMillis,
|
||||
DeleteEntry.class, deleteEntryResultMapper);
|
||||
SearchHit.class, deleteEntryResultMapper);
|
||||
} while (scrolledResult.getContent().size() != 0);
|
||||
|
||||
for (DeleteEntry entry : documentsToDelete) {
|
||||
bulkRequestBuilder.add(client.prepareDelete(entry.getIndexName(), typeName, entry.getId()));
|
||||
for (SearchHit entry : documentsToDelete) {
|
||||
bulkRequestBuilder.add(client.prepareDelete(entry.getIndex(), typeName, entry.getId()));
|
||||
}
|
||||
|
||||
if (bulkRequestBuilder.numberOfActions() > 0) {
|
||||
|
@ -52,7 +52,6 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
||||
import org.springframework.data.elasticsearch.core.query.*;
|
||||
import org.springframework.data.elasticsearch.entities.Book;
|
||||
import org.springframework.data.elasticsearch.entities.GTEVersionEntity;
|
||||
import org.springframework.data.elasticsearch.entities.HetroEntity1;
|
||||
import org.springframework.data.elasticsearch.entities.HetroEntity2;
|
||||
@ -400,6 +399,86 @@ public class ElasticsearchTemplateTests {
|
||||
assertThat(sampleEntities.getTotalElements(), equalTo(0L));
|
||||
}
|
||||
|
||||
@Test // DATAES-547
|
||||
public void shouldDeleteAcrossIndex() {
|
||||
|
||||
// given
|
||||
SampleEntity sampleEntity = SampleEntity.builder() //
|
||||
.message("foo") //
|
||||
.version(System.currentTimeMillis()) //
|
||||
.build();
|
||||
|
||||
IndexQuery idxQuery1 = new IndexQueryBuilder().withIndexName(INDEX_1_NAME).withId(randomNumeric(5))
|
||||
.withObject(sampleEntity).build();
|
||||
|
||||
elasticsearchTemplate.index(idxQuery1);
|
||||
elasticsearchTemplate.refresh(INDEX_1_NAME);
|
||||
|
||||
IndexQuery idxQuery2 = new IndexQueryBuilder().withIndexName(INDEX_2_NAME).withId(randomNumeric(5))
|
||||
.withObject(sampleEntity).build();
|
||||
|
||||
elasticsearchTemplate.index(idxQuery2);
|
||||
elasticsearchTemplate.refresh(INDEX_2_NAME);
|
||||
|
||||
// when
|
||||
DeleteQuery deleteQuery = new DeleteQuery();
|
||||
deleteQuery.setQuery(termQuery("message", "foo"));
|
||||
deleteQuery.setType("test-type");
|
||||
deleteQuery.setIndex("test-index-*");
|
||||
|
||||
elasticsearchTemplate.delete(deleteQuery);
|
||||
|
||||
elasticsearchTemplate.refresh(INDEX_1_NAME);
|
||||
elasticsearchTemplate.refresh(INDEX_2_NAME);
|
||||
|
||||
// then
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("message", "foo"))
|
||||
.withIndices(INDEX_1_NAME, INDEX_2_NAME) //
|
||||
.build();
|
||||
|
||||
assertThat(elasticsearchTemplate.count(searchQuery), equalTo(0L));
|
||||
}
|
||||
|
||||
@Test // DATAES-547
|
||||
public void shouldDeleteAcrossIndexWhenNoMatchingDataPresent() {
|
||||
|
||||
// given
|
||||
SampleEntity sampleEntity = SampleEntity.builder() //
|
||||
.message("positive") //
|
||||
.version(System.currentTimeMillis()) //
|
||||
.build();
|
||||
|
||||
IndexQuery idxQuery1 = new IndexQueryBuilder().withIndexName(INDEX_1_NAME).withId(randomNumeric(5))
|
||||
.withObject(sampleEntity).build();
|
||||
|
||||
elasticsearchTemplate.index(idxQuery1);
|
||||
elasticsearchTemplate.refresh(INDEX_1_NAME);
|
||||
|
||||
IndexQuery idxQuery2 = new IndexQueryBuilder().withIndexName(INDEX_2_NAME).withId(randomNumeric(5))
|
||||
.withObject(sampleEntity).build();
|
||||
|
||||
elasticsearchTemplate.index(idxQuery2);
|
||||
elasticsearchTemplate.refresh(INDEX_2_NAME);
|
||||
|
||||
// when
|
||||
DeleteQuery deleteQuery = new DeleteQuery();
|
||||
deleteQuery.setQuery(termQuery("message", "negative"));
|
||||
deleteQuery.setType("test-type");
|
||||
deleteQuery.setIndex("test-index-*");
|
||||
|
||||
elasticsearchTemplate.delete(deleteQuery);
|
||||
|
||||
elasticsearchTemplate.refresh(INDEX_1_NAME);
|
||||
elasticsearchTemplate.refresh(INDEX_2_NAME);
|
||||
|
||||
// then
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("message", "positive"))
|
||||
.withIndices(INDEX_1_NAME, INDEX_2_NAME) //
|
||||
.build();
|
||||
|
||||
assertThat(elasticsearchTemplate.count(searchQuery), equalTo(2L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFilterSearchResultsForGivenFilter() {
|
||||
// given
|
||||
|
Loading…
x
Reference in New Issue
Block a user