Skip to content

Commit 49324a3

Browse files
authored
Fix IndexOutOfBoundsException when try to map inner hits with no results returned.
Original Pull Request spring-projects#1998 Closes spring-projects#1997 Co-authored-by: Peter-Josef Meisch <[email protected]>
1 parent 44f9b29 commit 49324a3

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/core/SearchHitMapping.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import java.util.Map;
2323
import java.util.stream.Collectors;
2424

25-
import org.apache.commons.logging.Log;
26-
import org.apache.commons.logging.LogFactory;
25+
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
2726
import org.springframework.data.elasticsearch.backend.elasticsearch7.document.SearchDocumentResponse;
2827
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
2928
import org.springframework.data.elasticsearch.core.document.Document;
@@ -45,12 +44,11 @@
4544
* @author Mark Paluch
4645
* @author Roman Puchkovskiy
4746
* @author Matt Gilene
47+
* @author Sascha Woo
4848
* @since 4.0
4949
*/
5050
public class SearchHitMapping<T> {
5151

52-
private static final Log LOGGER = LogFactory.getLog(SearchHitMapping.class);
53-
5452
private final Class<T> type;
5553
private final ElasticsearchConverter converter;
5654
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
@@ -194,7 +192,7 @@ private Map<String, SearchHits<?>> mapInnerHits(SearchDocument searchDocument) {
194192
*/
195193
private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, Class<T> type) {
196194

197-
if (searchHits.getTotalHits() == 0) {
195+
if (searchHits.isEmpty()) {
198196
return searchHits;
199197
}
200198

@@ -239,7 +237,7 @@ private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, C
239237
searchHits.getSuggest());
240238
}
241239
} catch (Exception e) {
242-
LOGGER.warn("Could not map inner_hits", e);
240+
throw new UncategorizedElasticsearchException("Unable to convert inner hits.", e);
243241
}
244242

245243
return searchHits;

Diff for: src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java

+27
Original file line numberDiff line numberDiff line change
@@ -2787,6 +2787,33 @@ public void shouldReturnDocumentWithCollapsedFieldAndInnerHits() {
27872787
assertThat(searchHits.getSearchHit(1).getInnerHits("innerHits").getTotalHits()).isEqualTo(1);
27882788
}
27892789

2790+
@Test // #1997
2791+
@DisplayName("should return document with inner hits size zero")
2792+
void shouldReturnDocumentWithInnerHitsSizeZero() {
2793+
2794+
// given
2795+
SampleEntity sampleEntity = SampleEntity.builder().id(nextIdAsString()).message("message 1").rate(1)
2796+
.version(System.currentTimeMillis()).build();
2797+
2798+
List<IndexQuery> indexQueries = getIndexQueries(Arrays.asList(sampleEntity));
2799+
2800+
operations.bulkIndex(indexQueries, IndexCoordinates.of(indexNameProvider.indexName()));
2801+
2802+
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
2803+
.withCollapseBuilder(new CollapseBuilder("rate").setInnerHits(new InnerHitBuilder("innerHits").setSize(0)))
2804+
.build();
2805+
2806+
// when
2807+
SearchHits<SampleEntity> searchHits = operations.search(searchQuery, SampleEntity.class,
2808+
IndexCoordinates.of(indexNameProvider.indexName()));
2809+
2810+
// then
2811+
assertThat(searchHits).isNotNull();
2812+
assertThat(searchHits.getTotalHits()).isEqualTo(1);
2813+
assertThat(searchHits.getSearchHits()).hasSize(1);
2814+
assertThat(searchHits.getSearchHit(0).getContent().getMessage()).isEqualTo("message 1");
2815+
}
2816+
27902817
private IndexQuery getIndexQuery(SampleEntity sampleEntity) {
27912818
return new IndexQueryBuilder().withId(sampleEntity.getId()).withObject(sampleEntity)
27922819
.withVersion(sampleEntity.getVersion()).build();

0 commit comments

Comments
 (0)