Pageable results and @Query annotation.

Original Pull Request #1844
Closes #1843
This commit is contained in:
Peter-Josef Meisch 2021-06-15 22:13:39 +02:00 committed by GitHub
parent 73f11a0618
commit 93cf9ab794
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 5 deletions

View File

@ -31,7 +31,7 @@ import org.springframework.lang.Nullable;
/** /**
* Utility class with helper methods for working with {@link SearchHit}. * Utility class with helper methods for working with {@link SearchHit}.
* *
* @author Peter-Josef Meisch * @author Peter-Josef Meisch
* @author Sascha Woo * @author Sascha Woo
* @author Roman Puchkovskiy * @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 * 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 * @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 * @return a corresponding object where the SearchHits are replaced by their content if possible, otherwise the
* original object * original object
@ -86,6 +86,12 @@ public final class SearchHitSupport {
return unwrapSearchHitsIterator((SearchHitsIterator<?>) result); 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 (ReactiveWrappers.isAvailable(ReactiveWrappers.ReactiveLibrary.PROJECT_REACTOR)) {
if (result instanceof Flux) { 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. * Builds an {@link AggregatedPage} with the {@link SearchHit} objects from a {@link SearchHits} object.
* *
* @param searchHits, must not be {@literal null}. * @param searchHits, must not be {@literal null}.
* @param pageable, must not be {@literal null}. * @param pageable, must not be {@literal null}.
* @return the created Page * @return the created Page
@ -142,7 +148,7 @@ public final class SearchHitSupport {
/** /**
* SearchPage implementation. * SearchPage implementation.
* *
* @param <T> * @param <T>
*/ */
static class SearchPageImpl<T> extends PageImpl<SearchHit<T>> implements SearchPage<T> { static class SearchPageImpl<T> extends PageImpl<SearchHit<T>> implements SearchPage<T> {

View File

@ -126,7 +126,9 @@ public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery
result = elasticsearchOperations.searchOne(query, clazz, index); result = elasticsearchOperations.searchOne(query, clazz, index);
} }
return queryMethod.isNotSearchHitMethod() ? SearchHitSupport.unwrapSearchHits(result) : result; return (queryMethod.isNotSearchHitMethod() && !queryMethod.isSearchPageMethod())
? SearchHitSupport.unwrapSearchHits(result)
: result;
} }
@Nullable @Nullable

View File

@ -151,6 +151,7 @@ public abstract class CustomMethodRepositoryBaseTests {
// then // then
assertThat(page).isNotNull(); assertThat(page).isNotNull();
assertThat(page.getTotalElements()).isGreaterThanOrEqualTo(1L); assertThat(page.getTotalElements()).isGreaterThanOrEqualTo(1L);
assertThat(page.getContent().get(0)).isInstanceOf(SampleEntity.class);
} }
@Test @Test