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

(cherry picked from commit e717586)
(cherry picked from commit 2dd0a67)
  • Loading branch information
sothawo committed Sep 3, 2021
1 parent 1348ee3 commit aca34ee
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 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 @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -1587,6 +1587,19 @@ void shouldReturnSearchPageWithQuery() {
assertThat((nextPageable.getPageNumber())).isEqualTo(1);
}

@Test // #1917
void shouldReturnAllDocumentsWithUnpagedQuery() {

List<SampleEntity> entities = createSampleEntities("abc", 20);
repository.saveAll(entities);

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

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

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

List<SampleEntity> entities = new ArrayList<>();
Expand Down Expand Up @@ -1766,6 +1779,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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -572,6 +595,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 aca34ee

Please sign in to comment.