DATAES-777 - SearchHitsSupport must preserve pageable when unwrapping to AggregatedPage.

Original PR: #418
This commit is contained in:
Peter-Josef Meisch 2020-04-01 21:18:52 +02:00 committed by GitHub
parent bce9da4343
commit b434b05215
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 8 deletions

View File

@ -65,8 +65,8 @@ public final class SearchHitSupport {
if (result instanceof AggregatedPage<?>) {
AggregatedPage<?> page = (AggregatedPage<?>) result;
List<?> list = page.getContent().stream().map(SearchHitSupport::unwrapSearchHits).collect(Collectors.toList());
return new AggregatedPageImpl<>(list, null, page.getTotalElements(), page.getAggregations(), page.getScrollId(),
page.getMaxScore());
return new AggregatedPageImpl<>(list, page.getPageable(), page.getTotalElements(), page.getAggregations(),
page.getScrollId(), page.getMaxScore());
}

View File

@ -40,7 +40,7 @@ public class AggregatedPageImpl<T> extends PageImpl<T> implements AggregatedPage
@Nullable private String scrollId;
private float maxScore;
private static Pageable pageableOrUnpaged(Pageable pageable) {
private static Pageable pageableOrUnpaged(@Nullable Pageable pageable) {
return ofNullable(pageable).orElse(Pageable.unpaged());
}
@ -82,24 +82,25 @@ public class AggregatedPageImpl<T> extends PageImpl<T> implements AggregatedPage
this.maxScore = maxScore;
}
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, Aggregations aggregations) {
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, @Nullable Aggregations aggregations) {
super(content, pageableOrUnpaged(pageable), total);
this.aggregations = aggregations;
}
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, Aggregations aggregations, float maxScore) {
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, @Nullable Aggregations aggregations,
float maxScore) {
this(content, pageableOrUnpaged(pageable), total, aggregations);
this.maxScore = maxScore;
}
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, Aggregations aggregations,
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, @Nullable Aggregations aggregations,
String scrollId) {
this(content, pageableOrUnpaged(pageable), total, aggregations);
this.scrollId = scrollId;
}
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, Aggregations aggregations, String scrollId,
float maxScore) {
public AggregatedPageImpl(List<T> content, Pageable pageable, long total, @Nullable Aggregations aggregations,
String scrollId, float maxScore) {
this(content, pageableOrUnpaged(pageable), total, aggregations, scrollId);
this.maxScore = maxScore;
}

View File

@ -545,6 +545,37 @@ public abstract class CustomMethodRepositoryBaseTests {
assertThat(page.getTotalElements()).isEqualTo(1L);
}
@Test // DATAES-777
public void shouldReturnPageableInUnwrappedPageResult() {
// given
String documentId = randomNumeric(5);
SampleEntity sampleEntity = new SampleEntity();
sampleEntity.setId(documentId);
sampleEntity.setType("test");
sampleEntity.setMessage("foo");
sampleEntity.setAvailable(true);
repository.save(sampleEntity);
// given
String documentId2 = randomNumeric(5);
SampleEntity sampleEntity2 = new SampleEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setType("test");
sampleEntity2.setMessage("bar");
sampleEntity2.setAvailable(false);
repository.save(sampleEntity2);
// when
PageRequest pageable = PageRequest.of(0, 10);
Page<SampleEntity> page = repository.findByAvailable(false, pageable);
// then
assertThat(page).isNotNull();
assertThat(page.getTotalElements()).isEqualTo(1L);
assertThat(page.getPageable()).isSameAs(pageable);
}
@Test
public void shouldReturnPageableResultsWithQueryAnnotationExpectedPageSize() {