SearchPage result in StringQuery methods.

Original Pull Request #1812
Closes #1811

(cherry picked from commit e96d09fa51e7ee8450513657f51dbd55c4a54641)
(cherry picked from commit ad6022f64ca6071e1d139ae97c39b703570b49d2)
(cherry picked from commit 26a3b324b704dcc5985fc69beec61fc31045d349)
This commit is contained in:
Peter-Josef Meisch 2021-05-13 16:48:57 +02:00
parent 934086be81
commit a6adcef67d
No known key found for this signature in database
GPG Key ID: DE108246970C7708
2 changed files with 29 additions and 6 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2020 the original author or authors. * Copyright 2013-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,19 +15,16 @@
*/ */
package org.springframework.data.elasticsearch.repository.query; package org.springframework.data.elasticsearch.repository.query;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHitSupport; import org.springframework.data.elasticsearch.core.SearchHitSupport;
import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.convert.DateTimeConverters;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.StringQuery; import org.springframework.data.elasticsearch.core.query.StringQuery;
import org.springframework.data.elasticsearch.repository.support.StringQueryUtil; import org.springframework.data.elasticsearch.repository.support.StringQueryUtil;
import org.springframework.data.repository.query.ParametersParameterAccessor; import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.data.util.StreamUtils; import org.springframework.data.util.StreamUtils;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/** /**
* ElasticsearchStringQuery * ElasticsearchStringQuery
@ -69,7 +66,12 @@ public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQue
if (queryMethod.isPageQuery()) { if (queryMethod.isPageQuery()) {
stringQuery.setPageable(accessor.getPageable()); stringQuery.setPageable(accessor.getPageable());
SearchHits<?> searchHits = elasticsearchOperations.search(stringQuery, clazz, index); SearchHits<?> searchHits = elasticsearchOperations.search(stringQuery, clazz, index);
result = SearchHitSupport.page(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()) { } else if (queryMethod.isStreamQuery()) {
if (accessor.getPageable().isUnpaged()) { if (accessor.getPageable().isUnpaged()) {
stringQuery.setPageable(PageRequest.of(0, DEFAULT_STREAM_BATCH_SIZE)); stringQuery.setPageable(PageRequest.of(0, DEFAULT_STREAM_BATCH_SIZE));
@ -86,7 +88,9 @@ public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQue
result = elasticsearchOperations.searchOne(stringQuery, clazz, index); result = elasticsearchOperations.searchOne(stringQuery, clazz, index);
} }
return queryMethod.isNotSearchHitMethod() ? SearchHitSupport.unwrapSearchHits(result) : result; return (queryMethod.isNotSearchHitMethod() && !queryMethod.isSearchPageMethod())
? SearchHitSupport.unwrapSearchHits(result)
: result;
} }
protected StringQuery createQuery(ParametersParameterAccessor parameterAccessor) { protected StringQuery createQuery(ParametersParameterAccessor parameterAccessor) {

View File

@ -1570,6 +1570,22 @@ public abstract class CustomMethodRepositoryBaseTests {
assertThat((nextPageable.getPageNumber())).isEqualTo(1); assertThat((nextPageable.getPageNumber())).isEqualTo(1);
} }
@Test // #1811
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) { private List<SampleEntity> createSampleEntities(String type, int numberOfEntities) {
List<SampleEntity> entities = new ArrayList<>(); List<SampleEntity> entities = new ArrayList<>();
@ -1746,6 +1762,9 @@ public abstract class CustomMethodRepositoryBaseTests {
SearchHits<SampleEntity> searchBy(Sort sort); SearchHits<SampleEntity> searchBy(Sort sort);
SearchPage<SampleEntity> searchByMessage(String message, Pageable pageable); SearchPage<SampleEntity> searchByMessage(String message, Pageable pageable);
@Query("{\"match\": {\"message\": \"?0\"}}")
SearchPage<SampleEntity> searchWithQueryByMessage(String message, Pageable pageable);
} }
/** /**