mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-18 18:12:10 +00:00
Fix IndexOutOfBoundsException when try to map inner hits with no results returned.
Original Pull Request #1998 Closes #1997 Co-authored-by: Peter-Josef Meisch <pj.meisch@sothawo.com>
This commit is contained in:
parent
44f9b29b66
commit
49324a369a
@ -22,8 +22,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.springframework.data.elasticsearch.backend.elasticsearch7.document.SearchDocumentResponse;
|
import org.springframework.data.elasticsearch.backend.elasticsearch7.document.SearchDocumentResponse;
|
||||||
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
|
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
|
||||||
import org.springframework.data.elasticsearch.core.document.Document;
|
import org.springframework.data.elasticsearch.core.document.Document;
|
||||||
@ -45,12 +44,11 @@ import org.springframework.util.Assert;
|
|||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
* @author Roman Puchkovskiy
|
* @author Roman Puchkovskiy
|
||||||
* @author Matt Gilene
|
* @author Matt Gilene
|
||||||
|
* @author Sascha Woo
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
*/
|
*/
|
||||||
public class SearchHitMapping<T> {
|
public class SearchHitMapping<T> {
|
||||||
|
|
||||||
private static final Log LOGGER = LogFactory.getLog(SearchHitMapping.class);
|
|
||||||
|
|
||||||
private final Class<T> type;
|
private final Class<T> type;
|
||||||
private final ElasticsearchConverter converter;
|
private final ElasticsearchConverter converter;
|
||||||
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
||||||
@ -194,7 +192,7 @@ public class SearchHitMapping<T> {
|
|||||||
*/
|
*/
|
||||||
private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, Class<T> type) {
|
private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, Class<T> type) {
|
||||||
|
|
||||||
if (searchHits.getTotalHits() == 0) {
|
if (searchHits.isEmpty()) {
|
||||||
return searchHits;
|
return searchHits;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,7 +237,7 @@ public class SearchHitMapping<T> {
|
|||||||
searchHits.getSuggest());
|
searchHits.getSuggest());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOGGER.warn("Could not map inner_hits", e);
|
throw new UncategorizedElasticsearchException("Unable to convert inner hits.", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return searchHits;
|
return searchHits;
|
||||||
|
@ -2787,6 +2787,33 @@ public abstract class ElasticsearchTemplateTests {
|
|||||||
assertThat(searchHits.getSearchHit(1).getInnerHits("innerHits").getTotalHits()).isEqualTo(1);
|
assertThat(searchHits.getSearchHit(1).getInnerHits("innerHits").getTotalHits()).isEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // #1997
|
||||||
|
@DisplayName("should return document with inner hits size zero")
|
||||||
|
void shouldReturnDocumentWithInnerHitsSizeZero() {
|
||||||
|
|
||||||
|
// given
|
||||||
|
SampleEntity sampleEntity = SampleEntity.builder().id(nextIdAsString()).message("message 1").rate(1)
|
||||||
|
.version(System.currentTimeMillis()).build();
|
||||||
|
|
||||||
|
List<IndexQuery> indexQueries = getIndexQueries(Arrays.asList(sampleEntity));
|
||||||
|
|
||||||
|
operations.bulkIndex(indexQueries, IndexCoordinates.of(indexNameProvider.indexName()));
|
||||||
|
|
||||||
|
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||||
|
.withCollapseBuilder(new CollapseBuilder("rate").setInnerHits(new InnerHitBuilder("innerHits").setSize(0)))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// when
|
||||||
|
SearchHits<SampleEntity> searchHits = operations.search(searchQuery, SampleEntity.class,
|
||||||
|
IndexCoordinates.of(indexNameProvider.indexName()));
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(searchHits).isNotNull();
|
||||||
|
assertThat(searchHits.getTotalHits()).isEqualTo(1);
|
||||||
|
assertThat(searchHits.getSearchHits()).hasSize(1);
|
||||||
|
assertThat(searchHits.getSearchHit(0).getContent().getMessage()).isEqualTo("message 1");
|
||||||
|
}
|
||||||
|
|
||||||
private IndexQuery getIndexQuery(SampleEntity sampleEntity) {
|
private IndexQuery getIndexQuery(SampleEntity sampleEntity) {
|
||||||
return new IndexQueryBuilder().withId(sampleEntity.getId()).withObject(sampleEntity)
|
return new IndexQueryBuilder().withId(sampleEntity.getId()).withObject(sampleEntity)
|
||||||
.withVersion(sampleEntity.getVersion()).build();
|
.withVersion(sampleEntity.getVersion()).build();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user