diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchStringQuery.java b/src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchStringQuery.java index 58d68a01e..0b35902c2 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchStringQuery.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchStringQuery.java @@ -16,6 +16,7 @@ package org.springframework.data.elasticsearch.repository.query; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.SearchHitSupport; import org.springframework.data.elasticsearch.core.SearchHits; @@ -73,16 +74,11 @@ public Object execute(Object[] parameters) { .unwrapSearchHits(SearchHitSupport.searchPageFor(searchHits, stringQuery.getPageable())); } } else if (queryMethod.isStreamQuery()) { - if (accessor.getPageable().isUnpaged()) { - stringQuery.setPageable(PageRequest.of(0, DEFAULT_STREAM_BATCH_SIZE)); - } else { - stringQuery.setPageable(accessor.getPageable()); - } + stringQuery.setPageable( + accessor.getPageable().isPaged() ? accessor.getPageable() : PageRequest.of(0, DEFAULT_STREAM_BATCH_SIZE)); result = StreamUtils.createStreamFromIterator(elasticsearchOperations.searchForStream(stringQuery, clazz, index)); } else if (queryMethod.isCollectionQuery()) { - if (accessor.getPageable().isPaged()) { - stringQuery.setPageable(accessor.getPageable()); - } + stringQuery.setPageable(accessor.getPageable().isPaged() ? accessor.getPageable() : Pageable.unpaged()); result = elasticsearchOperations.search(stringQuery, clazz, index); } else { result = elasticsearchOperations.searchOne(stringQuery, clazz, index); 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 536df5ae9..3b9142101 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 @@ -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"); * you may not use this file except in compliance with the License. @@ -1587,6 +1587,19 @@ void shouldReturnSearchPageWithQuery() { assertThat((nextPageable.getPageNumber())).isEqualTo(1); } + @Test // #1917 + void shouldReturnAllDocumentsWithUnpagedQuery() { + + List entities = createSampleEntities("abc", 20); + repository.saveAll(entities); + + SearchHits searchHits = repository.searchWithQueryByMessageUnpaged("Message"); + + assertThat(searchHits).isNotNull(); + assertThat((searchHits.getTotalHits())).isEqualTo(20); + assertThat(searchHits.getSearchHits()).hasSize(20); + } + private List createSampleEntities(String type, int numberOfEntities) { List entities = new ArrayList<>(); @@ -1766,6 +1779,9 @@ public interface SampleCustomMethodRepository extends ElasticsearchRepository searchWithQueryByMessage(String message, Pageable pageable); + + @Query("{\"match\": {\"message\": \"?0\"}}") + SearchHits searchWithQueryByMessageUnpaged(String message); } /** diff --git a/src/test/java/org/springframework/data/elasticsearch/repository/support/SimpleReactiveElasticsearchRepositoryTests.java b/src/test/java/org/springframework/data/elasticsearch/repository/support/SimpleReactiveElasticsearchRepositoryTests.java index 1203bea49..c1ce10827 100644 --- a/src/test/java/org/springframework/data/elasticsearch/repository/support/SimpleReactiveElasticsearchRepositoryTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/repository/support/SimpleReactiveElasticsearchRepositoryTests.java @@ -31,6 +31,7 @@ import java.lang.Long; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import java.util.stream.IntStream; import org.elasticsearch.ElasticsearchStatusException; @@ -515,16 +516,38 @@ public void derivedFinderMethodReturningMonoShouldBeExecutedCorrectly() { } @Test // DATAES-519 - public void annotatedFinderMethodShouldBeExecutedCorrectly() { + void annotatedFinderMethodShouldBeExecutedCorrectly() { - bulkIndex(SampleEntity.builder().id("id-one").message("message").build(), // - SampleEntity.builder().id("id-two").message("test message").build(), // - SampleEntity.builder().id("id-three").message("test test").build()) // - .block(); + int count = 30; + SampleEntity[] sampleEntities = IntStream.range(1, count + 1) + .mapToObj(i -> SampleEntity.builder().id("id-" + i).message("test " + i).build()).collect(Collectors.toList()) + .toArray(new SampleEntity[count]); + + bulkIndex(sampleEntities).block(); repository.findAllViaAnnotatedQueryByMessageLike("test") // .as(StepVerifier::create) // - .expectNextCount(2) // + .expectNextCount(count) // + .verifyComplete(); + } + + @Test // #1917 + void annotatedFinderMethodPagedShouldBeExecutedCorrectly() { + + int count = 30; + SampleEntity[] sampleEntities = IntStream.range(1, count + 1) + .mapToObj(i -> SampleEntity.builder().id("id-" + i).message("test " + i).build()).collect(Collectors.toList()) + .toArray(new SampleEntity[count]); + + bulkIndex(sampleEntities).block(); + + repository.findAllViaAnnotatedQueryByMessageLikePaged("test", PageRequest.of(0, 20)) // + .as(StepVerifier::create) // + .expectNextCount(20) // + .verifyComplete(); + repository.findAllViaAnnotatedQueryByMessageLikePaged("test", PageRequest.of(1, 20)) // + .as(StepVerifier::create) // + .expectNextCount(10) // .verifyComplete(); } @@ -572,6 +595,9 @@ interface ReactiveSampleEntityRepository extends ReactiveCrudRepository findAllViaAnnotatedQueryByMessageLike(String message); + @Query("{ \"bool\" : { \"must\" : { \"term\" : { \"message\" : \"?0\" } } } }") + Flux findAllViaAnnotatedQueryByMessageLikePaged(String message, Pageable pageable); + Mono findFirstByMessageLike(String message); Mono countAllByMessage(String message);