Skip to content

Commit

Permalink
Fix @query method implementation for unpaged queries.
Browse files Browse the repository at this point in the history
Original Pull Request #1919 
Closes #1917
  • Loading branch information
sothawo authored Sep 3, 2021
1 parent 35c7ef4 commit e717586
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,16 +82,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1612,19 +1612,18 @@ void shouldReturnSearchPageWithQuery() {
assertThat((nextPageable.getPageNumber())).isEqualTo(1);
}

private List<SampleEntity> createSampleEntities(String type, int numberOfEntities) {
@Test // #1917
@DisplayName("shouldReturnAllDocumentsWithUnpagedQuery")
void shouldReturnAllDocumentsWithUnpagedQuery() {

List<SampleEntity> entities = new ArrayList<>();
for (int i = 0; i < numberOfEntities; i++) {
SampleEntity entity = new SampleEntity();
entity.setId(UUID.randomUUID().toString());
entity.setAvailable(true);
entity.setMessage("Message");
entity.setType(type);
entities.add(entity);
}
List<SampleEntity> entities = createSampleEntities("abc", 20);
repository.saveAll(entities);

return entities;
SearchHits<SampleEntity> searchHits = repository.searchWithQueryByMessageUnpaged("Message");

assertThat(searchHits).isNotNull();
assertThat((searchHits.getTotalHits())).isEqualTo(20);
assertThat(searchHits.getSearchHits()).hasSize(20);
}

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

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);
entity.setMessage("Message");
entity.setType(type);
entities.add(entity);
}

return entities;
}

@Document(indexName = "#{@indexNameProvider.indexName()}")
static class SampleEntity {
@Nullable @Id private String id;
Expand Down Expand Up @@ -1857,6 +1871,9 @@ public interface SampleCustomMethodRepository extends ElasticsearchRepository<Sa
@Query("{\"match\": {\"message\": \"?0\"}}")
SearchPage<SampleEntity> searchWithQueryByMessage(String message, Pageable pageable);

@Query("{\"match\": {\"message\": \"?0\"}}")
SearchHits<SampleEntity> searchWithQueryByMessageUnpaged(String message);

@CountQuery("{\"bool\" : {\"must\" : {\"term\" : {\"type\" : \"?0\"}}}}")
long countWithQueryByType(String type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.elasticsearch.ElasticsearchStatusException;
Expand Down Expand Up @@ -541,14 +542,36 @@ void derivedFinderMethodReturningMonoShouldBeExecutedCorrectly() {
@Test // DATAES-519
void annotatedFinderMethodShouldBeExecutedCorrectly() {

bulkIndex(new SampleEntity("id-one", "message"), //
new SampleEntity("id-two", "test message"), //
new SampleEntity("id-three", "test test")) //
.block();
int count = 30;
SampleEntity[] sampleEntities = IntStream.range(1, count + 1)
.mapToObj(i -> new SampleEntity("id-" + i, "test " + i)).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 -> new SampleEntity("id-" + i, "test " + i)).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();
}

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

@Query("{ \"bool\" : { \"must\" : { \"term\" : { \"message\" : \"?0\" } } } }")
Flux<SampleEntity> findAllViaAnnotatedQueryByMessageLikePaged(String message, Pageable pageable);

Mono<SampleEntity> findFirstByMessageLike(String message);

Mono<Long> countAllByMessage(String message);
Expand Down

0 comments on commit e717586

Please sign in to comment.