diff --git a/src/main/java/org/springframework/data/elasticsearch/core/SearchHitSupport.java b/src/main/java/org/springframework/data/elasticsearch/core/SearchHitSupport.java index 39b3b65a9..38d30c7f7 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/SearchHitSupport.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/SearchHitSupport.java @@ -31,7 +31,7 @@ import org.springframework.lang.Nullable; /** * Utility class with helper methods for working with {@link SearchHit}. - * + * * @author Peter-Josef Meisch * @author Sascha Woo * @author Roman Puchkovskiy @@ -43,7 +43,7 @@ public final class SearchHitSupport { /** * unwraps the data contained in a SearchHit for different types containing SearchHits if possible - * + * * @param result the object, list, page or whatever containing SearchHit objects * @return a corresponding object where the SearchHits are replaced by their content if possible, otherwise the * original object @@ -86,6 +86,12 @@ public final class SearchHitSupport { return unwrapSearchHitsIterator((SearchHitsIterator) result); } + if (result instanceof SearchPage) { + SearchPage searchPage = (SearchPage) result; + List content = (List) SearchHitSupport.unwrapSearchHits(searchPage.getSearchHits()); + return new PageImpl<>(content, searchPage.getPageable(), searchPage.getTotalElements()); + } + if (ReactiveWrappers.isAvailable(ReactiveWrappers.ReactiveLibrary.PROJECT_REACTOR)) { if (result instanceof Flux) { @@ -119,7 +125,7 @@ public final class SearchHitSupport { /** * Builds an {@link AggregatedPage} with the {@link SearchHit} objects from a {@link SearchHits} object. - * + * * @param searchHits, must not be {@literal null}. * @param pageable, must not be {@literal null}. * @return the created Page @@ -142,7 +148,7 @@ public final class SearchHitSupport { /** * SearchPage implementation. - * + * * @param */ static class SearchPageImpl extends PageImpl> implements SearchPage { diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchPartQuery.java b/src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchPartQuery.java index 703c7d7ab..81293999e 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchPartQuery.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchPartQuery.java @@ -126,7 +126,9 @@ public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery result = elasticsearchOperations.searchOne(query, clazz, index); } - return queryMethod.isNotSearchHitMethod() ? SearchHitSupport.unwrapSearchHits(result) : result; + return (queryMethod.isNotSearchHitMethod() && !queryMethod.isSearchPageMethod()) + ? SearchHitSupport.unwrapSearchHits(result) + : result; } @Nullable diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/custommethod/CustomMethodRepositoryBaseTests.java b/src/test/java/org/springframework/data/elasticsearch/repositories/custommethod/CustomMethodRepositoryBaseTests.java index 1ad171a1e..536df5ae9 100644 --- a/src/test/java/org/springframework/data/elasticsearch/repositories/custommethod/CustomMethodRepositoryBaseTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/custommethod/CustomMethodRepositoryBaseTests.java @@ -151,6 +151,7 @@ public abstract class CustomMethodRepositoryBaseTests { // then assertThat(page).isNotNull(); assertThat(page.getTotalElements()).isGreaterThanOrEqualTo(1L); + assertThat(page.getContent().get(0)).isInstanceOf(SampleEntity.class); } @Test