|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.elasticsearch.core.paginating; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import lombok.AllArgsConstructor; |
| 21 | +import lombok.Builder; |
| 22 | +import lombok.Data; |
| 23 | +import lombok.NoArgsConstructor; |
| 24 | +import reactor.core.publisher.Mono; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.stream.Collectors; |
| 29 | +import java.util.stream.IntStream; |
| 30 | + |
| 31 | +import org.junit.jupiter.api.DisplayName; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.data.annotation.Id; |
| 35 | +import org.springframework.data.domain.PageRequest; |
| 36 | +import org.springframework.data.domain.Sort; |
| 37 | +import org.springframework.data.elasticsearch.annotations.Document; |
| 38 | +import org.springframework.data.elasticsearch.annotations.Field; |
| 39 | +import org.springframework.data.elasticsearch.annotations.FieldType; |
| 40 | +import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations; |
| 41 | +import org.springframework.data.elasticsearch.core.SearchHit; |
| 42 | +import org.springframework.data.elasticsearch.core.query.Query; |
| 43 | +import org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchRestTemplateConfiguration; |
| 44 | +import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest; |
| 45 | +import org.springframework.test.context.ContextConfiguration; |
| 46 | + |
| 47 | +/** |
| 48 | + * @author Peter-Josef Meisch |
| 49 | + */ |
| 50 | +@SpringIntegrationTest |
| 51 | +@ContextConfiguration(classes = { ReactiveElasticsearchRestTemplateConfiguration.class }) |
| 52 | +public class ReactiveSearchAfterIntegrationTests { |
| 53 | + |
| 54 | + @Autowired private ReactiveElasticsearchOperations operations; |
| 55 | + |
| 56 | + @Test // #1143 |
| 57 | + @DisplayName("should read pages with search_after") |
| 58 | + void shouldReadPagesWithSearchAfter() { |
| 59 | + |
| 60 | + List<Entity> entities = IntStream.rangeClosed(1, 10) |
| 61 | + .mapToObj(i -> Entity.builder().id((long) i).message("message " + i).build()).collect(Collectors.toList()); |
| 62 | + operations.saveAll(Mono.just(entities), Entity.class).blockLast(); |
| 63 | + |
| 64 | + Query query = Query.findAll(); |
| 65 | + query.setPageable(PageRequest.of(0, 3)); |
| 66 | + query.addSort(Sort.by(Sort.Direction.ASC, "id")); |
| 67 | + |
| 68 | + List<Object> searchAfter = null; |
| 69 | + List<Entity> foundEntities = new ArrayList<>(); |
| 70 | + |
| 71 | + int loop = 0; |
| 72 | + do { |
| 73 | + query.setSearchAfter(searchAfter); |
| 74 | + List<SearchHit<Entity>> searchHits = operations.search(query, Entity.class).collectList().block(); |
| 75 | + |
| 76 | + if (searchHits.size() == 0) { |
| 77 | + break; |
| 78 | + } |
| 79 | + foundEntities.addAll(searchHits.stream().map(searchHit -> searchHit.getContent()).collect(Collectors.toList())); |
| 80 | + searchAfter = searchHits.get((int) (searchHits.size() - 1)).getSortValues(); |
| 81 | + |
| 82 | + if (++loop > 10) { |
| 83 | + fail("loop not terminating"); |
| 84 | + } |
| 85 | + } while (true); |
| 86 | + |
| 87 | + assertThat(foundEntities).containsExactlyElementsOf(entities); |
| 88 | + } |
| 89 | + |
| 90 | + @Data |
| 91 | + @AllArgsConstructor |
| 92 | + @NoArgsConstructor |
| 93 | + @Builder |
| 94 | + @Document(indexName = "test-search-after") |
| 95 | + private static class Entity { |
| 96 | + @Id private Long id; |
| 97 | + @Field(type = FieldType.Text) private String message; |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments