Skip to content

SearchPage result in StringQuery methods. #1812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ public Object execute(Object[] parameters) {
} else if (queryMethod.isPageQuery()) {
stringQuery.setPageable(accessor.getPageable());
SearchHits<?> searchHits = elasticsearchOperations.search(stringQuery, clazz, index);
result = SearchHitSupport.searchPageFor(searchHits, stringQuery.getPageable());
if (queryMethod.isSearchPageMethod()) {
result = SearchHitSupport.searchPageFor(searchHits, stringQuery.getPageable());
} else {
result = SearchHitSupport
.unwrapSearchHits(SearchHitSupport.searchPageFor(searchHits, stringQuery.getPageable()));
}
} else if (queryMethod.isStreamQuery()) {
if (accessor.getPageable().isUnpaged()) {
stringQuery.setPageable(PageRequest.of(0, DEFAULT_STREAM_BATCH_SIZE));
Expand All @@ -91,7 +96,9 @@ public Object execute(Object[] parameters) {
result = elasticsearchOperations.searchOne(stringQuery, clazz, index);
}

return queryMethod.isNotSearchHitMethod() ? SearchHitSupport.unwrapSearchHits(result) : result;
return (queryMethod.isNotSearchHitMethod() && queryMethod.isNotSearchPageMethod())
? SearchHitSupport.unwrapSearchHits(result)
: result;
}

protected StringQuery createQuery(ParametersParameterAccessor parameterAccessor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1593,11 +1593,27 @@ void shouldReturnSearchPage() {
assertThat((nextPageable.getPageNumber())).isEqualTo(1);
}

@Test // #1811
@DisplayName("should return SearchPage with query")
void shouldReturnSearchPageWithQuery() {
List<SampleEntity> entities = createSampleEntities("abc", 20);
repository.saveAll(entities);

SearchPage<SampleEntity> searchPage = repository.searchWithQueryByMessage("Message", PageRequest.of(0, 10));

assertThat(searchPage).isNotNull();
SearchHits<SampleEntity> searchHits = searchPage.getSearchHits();
assertThat(searchHits).isNotNull();
assertThat((searchHits.getTotalHits())).isEqualTo(20);
assertThat(searchHits.getSearchHits()).hasSize(10);
Pageable nextPageable = searchPage.nextPageable();
assertThat((nextPageable.getPageNumber())).isEqualTo(1);
}

private List<SampleEntity> createSampleEntities(String type, int numberOfEntities) {

List<SampleEntity> entities = new ArrayList<>();
for (int i = 0; i < numberOfEntities; i++) {

SampleEntity entity = new SampleEntity();
entity.setId(UUID.randomUUID().toString());
entity.setAvailable(true);
Expand Down Expand Up @@ -1633,8 +1649,7 @@ void shouldStreamSearchHitsWithQueryAnnotatedMethod() {

@Document(indexName = "test-index-sample-repositories-custom-method")
static class SampleEntity {
@Nullable
@Id private String id;
@Nullable @Id private String id;
@Nullable @Field(type = Text, store = true, fielddata = true) private String type;
@Nullable @Field(type = Text, store = true, fielddata = true) private String message;
@Nullable @Field(type = Keyword) private String keyword;
Expand Down Expand Up @@ -1836,6 +1851,9 @@ public interface SampleCustomMethodRepository extends ElasticsearchRepository<Sa

SearchPage<SampleEntity> searchByMessage(String message, Pageable pageable);

@Query("{\"match\": {\"message\": \"?0\"}}")
SearchPage<SampleEntity> searchWithQueryByMessage(String message, Pageable pageable);

@CountQuery("{\"bool\" : {\"must\" : {\"term\" : {\"type\" : \"?0\"}}}}")
long countWithQueryByType(String type);
}
Expand Down