Skip to content

Commit 93cf9ab

Browse files
authored
Pageable results and @query annotation.
Original Pull Request #1844 Closes #1843
1 parent 73f11a0 commit 93cf9ab

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/main/java/org/springframework/data/elasticsearch/core/SearchHitSupport.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
/**
3333
* Utility class with helper methods for working with {@link SearchHit}.
34-
*
34+
*
3535
* @author Peter-Josef Meisch
3636
* @author Sascha Woo
3737
* @author Roman Puchkovskiy
@@ -43,7 +43,7 @@ private SearchHitSupport() {}
4343

4444
/**
4545
* unwraps the data contained in a SearchHit for different types containing SearchHits if possible
46-
*
46+
*
4747
* @param result the object, list, page or whatever containing SearchHit objects
4848
* @return a corresponding object where the SearchHits are replaced by their content if possible, otherwise the
4949
* original object
@@ -86,6 +86,12 @@ public static Object unwrapSearchHits(@Nullable Object result) {
8686
return unwrapSearchHitsIterator((SearchHitsIterator<?>) result);
8787
}
8888

89+
if (result instanceof SearchPage<?>) {
90+
SearchPage<?> searchPage = (SearchPage<?>) result;
91+
List<?> content = (List<?>) SearchHitSupport.unwrapSearchHits(searchPage.getSearchHits());
92+
return new PageImpl<>(content, searchPage.getPageable(), searchPage.getTotalElements());
93+
}
94+
8995
if (ReactiveWrappers.isAvailable(ReactiveWrappers.ReactiveLibrary.PROJECT_REACTOR)) {
9096

9197
if (result instanceof Flux) {
@@ -119,7 +125,7 @@ public void close() {
119125

120126
/**
121127
* Builds an {@link AggregatedPage} with the {@link SearchHit} objects from a {@link SearchHits} object.
122-
*
128+
*
123129
* @param searchHits, must not be {@literal null}.
124130
* @param pageable, must not be {@literal null}.
125131
* @return the created Page
@@ -142,7 +148,7 @@ public static <T> SearchPage<T> searchPageFor(SearchHits<T> searchHits, @Nullabl
142148

143149
/**
144150
* SearchPage implementation.
145-
*
151+
*
146152
* @param <T>
147153
*/
148154
static class SearchPageImpl<T> extends PageImpl<SearchHit<T>> implements SearchPage<T> {

src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchPartQuery.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ public Object execute(Object[] parameters) {
126126
result = elasticsearchOperations.searchOne(query, clazz, index);
127127
}
128128

129-
return queryMethod.isNotSearchHitMethod() ? SearchHitSupport.unwrapSearchHits(result) : result;
129+
return (queryMethod.isNotSearchHitMethod() && !queryMethod.isSearchPageMethod())
130+
? SearchHitSupport.unwrapSearchHits(result)
131+
: result;
130132
}
131133

132134
@Nullable

src/test/java/org/springframework/data/elasticsearch/repositories/custommethod/CustomMethodRepositoryBaseTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public void shouldExecuteCustomMethodWithQuery() {
151151
// then
152152
assertThat(page).isNotNull();
153153
assertThat(page.getTotalElements()).isGreaterThanOrEqualTo(1L);
154+
assertThat(page.getContent().get(0)).isInstanceOf(SampleEntity.class);
154155
}
155156

156157
@Test

0 commit comments

Comments
 (0)