From a201ff1e6603739905e5e532f8a49112c33be5bd Mon Sep 17 00:00:00 2001 From: Peter-Josef Meisch Date: Tue, 15 Jun 2021 22:11:58 +0200 Subject: [PATCH] Pageable results and @Query annotation. --- .../data/elasticsearch/core/SearchHitSupport.java | 14 ++++++++++---- .../repository/query/ElasticsearchPartQuery.java | 4 +++- .../CustomMethodRepositoryBaseTests.java | 1 + 3 files changed, 14 insertions(+), 5 deletions(-) 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 @@ /** * Utility class with helper methods for working with {@link SearchHit}. - * + * * @author Peter-Josef Meisch * @author Sascha Woo * @author Roman Puchkovskiy @@ -43,7 +43,7 @@ private 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 static Object unwrapSearchHits(@Nullable Object 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 (result instanceof Flux) { @@ -119,7 +125,7 @@ public void close() { /** * 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 static SearchPage searchPageFor(SearchHits searchHits, @Nullabl /** * 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 Object execute(Object[] parameters) { 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 void shouldExecuteCustomMethodWithQuery() { // then assertThat(page).isNotNull(); assertThat(page.getTotalElements()).isGreaterThanOrEqualTo(1L); + assertThat(page.getContent().get(0)).isInstanceOf(SampleEntity.class); } @Test