Skip to content

Commit e717586

Browse files
authored
Fix @query method implementation for unpaged queries.
Original Pull Request #1919 Closes #1917
1 parent 35c7ef4 commit e717586

File tree

3 files changed

+63
-24
lines changed

3 files changed

+63
-24
lines changed

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.elasticsearch.repository.query;
1717

1818
import org.springframework.data.domain.PageRequest;
19+
import org.springframework.data.domain.Pageable;
1920
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
2021
import org.springframework.data.elasticsearch.core.SearchHitSupport;
2122
import org.springframework.data.elasticsearch.core.SearchHits;
@@ -81,16 +82,11 @@ public Object execute(Object[] parameters) {
8182
.unwrapSearchHits(SearchHitSupport.searchPageFor(searchHits, stringQuery.getPageable()));
8283
}
8384
} else if (queryMethod.isStreamQuery()) {
84-
if (accessor.getPageable().isUnpaged()) {
85-
stringQuery.setPageable(PageRequest.of(0, DEFAULT_STREAM_BATCH_SIZE));
86-
} else {
87-
stringQuery.setPageable(accessor.getPageable());
88-
}
85+
stringQuery.setPageable(
86+
accessor.getPageable().isPaged() ? accessor.getPageable() : PageRequest.of(0, DEFAULT_STREAM_BATCH_SIZE));
8987
result = StreamUtils.createStreamFromIterator(elasticsearchOperations.searchForStream(stringQuery, clazz, index));
9088
} else if (queryMethod.isCollectionQuery()) {
91-
if (accessor.getPageable().isPaged()) {
92-
stringQuery.setPageable(accessor.getPageable());
93-
}
89+
stringQuery.setPageable(accessor.getPageable().isPaged() ? accessor.getPageable() : Pageable.unpaged());
9490
result = elasticsearchOperations.search(stringQuery, clazz, index);
9591
} else {
9692
result = elasticsearchOperations.searchOne(stringQuery, clazz, index);

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

+28-11
Original file line numberDiff line numberDiff line change
@@ -1612,19 +1612,18 @@ void shouldReturnSearchPageWithQuery() {
16121612
assertThat((nextPageable.getPageNumber())).isEqualTo(1);
16131613
}
16141614

1615-
private List<SampleEntity> createSampleEntities(String type, int numberOfEntities) {
1615+
@Test // #1917
1616+
@DisplayName("shouldReturnAllDocumentsWithUnpagedQuery")
1617+
void shouldReturnAllDocumentsWithUnpagedQuery() {
16161618

1617-
List<SampleEntity> entities = new ArrayList<>();
1618-
for (int i = 0; i < numberOfEntities; i++) {
1619-
SampleEntity entity = new SampleEntity();
1620-
entity.setId(UUID.randomUUID().toString());
1621-
entity.setAvailable(true);
1622-
entity.setMessage("Message");
1623-
entity.setType(type);
1624-
entities.add(entity);
1625-
}
1619+
List<SampleEntity> entities = createSampleEntities("abc", 20);
1620+
repository.saveAll(entities);
16261621

1627-
return entities;
1622+
SearchHits<SampleEntity> searchHits = repository.searchWithQueryByMessageUnpaged("Message");
1623+
1624+
assertThat(searchHits).isNotNull();
1625+
assertThat((searchHits.getTotalHits())).isEqualTo(20);
1626+
assertThat(searchHits.getSearchHits()).hasSize(20);
16281627
}
16291628

16301629
@Test // DATAES-891
@@ -1649,6 +1648,21 @@ void shouldStreamSearchHitsWithQueryAnnotatedMethod() {
16491648
assertThat(count).isEqualTo(20);
16501649
}
16511650

1651+
private List<SampleEntity> createSampleEntities(String type, int numberOfEntities) {
1652+
1653+
List<SampleEntity> entities = new ArrayList<>();
1654+
for (int i = 0; i < numberOfEntities; i++) {
1655+
SampleEntity entity = new SampleEntity();
1656+
entity.setId(UUID.randomUUID().toString());
1657+
entity.setAvailable(true);
1658+
entity.setMessage("Message");
1659+
entity.setType(type);
1660+
entities.add(entity);
1661+
}
1662+
1663+
return entities;
1664+
}
1665+
16521666
@Document(indexName = "#{@indexNameProvider.indexName()}")
16531667
static class SampleEntity {
16541668
@Nullable @Id private String id;
@@ -1857,6 +1871,9 @@ public interface SampleCustomMethodRepository extends ElasticsearchRepository<Sa
18571871
@Query("{\"match\": {\"message\": \"?0\"}}")
18581872
SearchPage<SampleEntity> searchWithQueryByMessage(String message, Pageable pageable);
18591873

1874+
@Query("{\"match\": {\"message\": \"?0\"}}")
1875+
SearchHits<SampleEntity> searchWithQueryByMessageUnpaged(String message);
1876+
18601877
@CountQuery("{\"bool\" : {\"must\" : {\"term\" : {\"type\" : \"?0\"}}}}")
18611878
long countWithQueryByType(String type);
18621879
}

src/test/java/org/springframework/data/elasticsearch/repository/support/SimpleReactiveElasticsearchRepositoryTests.java

+31-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Arrays;
2929
import java.util.Collections;
3030
import java.util.List;
31+
import java.util.stream.Collectors;
3132
import java.util.stream.IntStream;
3233

3334
import org.elasticsearch.ElasticsearchStatusException;
@@ -541,14 +542,36 @@ void derivedFinderMethodReturningMonoShouldBeExecutedCorrectly() {
541542
@Test // DATAES-519
542543
void annotatedFinderMethodShouldBeExecutedCorrectly() {
543544

544-
bulkIndex(new SampleEntity("id-one", "message"), //
545-
new SampleEntity("id-two", "test message"), //
546-
new SampleEntity("id-three", "test test")) //
547-
.block();
545+
int count = 30;
546+
SampleEntity[] sampleEntities = IntStream.range(1, count + 1)
547+
.mapToObj(i -> new SampleEntity("id-" + i, "test " + i)).collect(Collectors.toList())
548+
.toArray(new SampleEntity[count]);
549+
550+
bulkIndex(sampleEntities).block();
548551

549552
repository.findAllViaAnnotatedQueryByMessageLike("test") //
550553
.as(StepVerifier::create) //
551-
.expectNextCount(2) //
554+
.expectNextCount(count) //
555+
.verifyComplete();
556+
}
557+
558+
@Test // #1917
559+
void annotatedFinderMethodPagedShouldBeExecutedCorrectly() {
560+
561+
int count = 30;
562+
SampleEntity[] sampleEntities = IntStream.range(1, count + 1)
563+
.mapToObj(i -> new SampleEntity("id-" + i, "test " + i)).collect(Collectors.toList())
564+
.toArray(new SampleEntity[count]);
565+
566+
bulkIndex(sampleEntities).block();
567+
568+
repository.findAllViaAnnotatedQueryByMessageLikePaged("test", PageRequest.of(0, 20)) //
569+
.as(StepVerifier::create) //
570+
.expectNextCount(20) //
571+
.verifyComplete();
572+
repository.findAllViaAnnotatedQueryByMessageLikePaged("test", PageRequest.of(1, 20)) //
573+
.as(StepVerifier::create) //
574+
.expectNextCount(10) //
552575
.verifyComplete();
553576
}
554577

@@ -596,6 +619,9 @@ interface ReactiveSampleEntityRepository extends ReactiveCrudRepository<SampleEn
596619
@Query("{ \"bool\" : { \"must\" : { \"term\" : { \"message\" : \"?0\" } } } }")
597620
Flux<SampleEntity> findAllViaAnnotatedQueryByMessageLike(String message);
598621

622+
@Query("{ \"bool\" : { \"must\" : { \"term\" : { \"message\" : \"?0\" } } } }")
623+
Flux<SampleEntity> findAllViaAnnotatedQueryByMessageLikePaged(String message, Pageable pageable);
624+
599625
Mono<SampleEntity> findFirstByMessageLike(String message);
600626

601627
Mono<Long> countAllByMessage(String message);

0 commit comments

Comments
 (0)