|
15 | 15 | */
|
16 | 16 | package org.springframework.data.elasticsearch.core.paginating;
|
17 | 17 |
|
18 |
| -import static org.assertj.core.api.Assertions.*; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.fail; |
19 | 20 |
|
20 | 21 | import java.util.ArrayList;
|
21 | 22 | import java.util.List;
|
22 | 23 | import java.util.stream.Collectors;
|
23 | 24 | import java.util.stream.IntStream;
|
24 | 25 |
|
| 26 | +import org.elasticsearch.search.sort.SortBuilders; |
| 27 | +import org.elasticsearch.search.sort.SortOrder; |
25 | 28 | import org.junit.jupiter.api.DisplayName;
|
26 | 29 | import org.junit.jupiter.api.Test;
|
27 | 30 | import org.springframework.beans.factory.annotation.Autowired;
|
|
33 | 36 | import org.springframework.data.elasticsearch.annotations.FieldType;
|
34 | 37 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
35 | 38 | import org.springframework.data.elasticsearch.core.SearchHits;
|
| 39 | +import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; |
36 | 40 | import org.springframework.data.elasticsearch.core.query.Query;
|
37 | 41 | import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
|
38 | 42 | import org.springframework.lang.Nullable;
|
@@ -79,6 +83,41 @@ void shouldReadPagesWithSearchAfter() {
|
79 | 83 | assertThat(foundEntities).containsExactlyElementsOf(entities);
|
80 | 84 | }
|
81 | 85 |
|
| 86 | + @Test // #2105 |
| 87 | + @DisplayName("should read pages with search_after using native search query") |
| 88 | + void shouldReadPagesWithSearchAfterUsingNativeSearchQuery() { |
| 89 | + |
| 90 | + List<Entity> entities = IntStream.rangeClosed(1, 10).mapToObj(i -> new Entity((long) i, "message " + i)) |
| 91 | + .collect(Collectors.toList()); |
| 92 | + operations.save(entities); |
| 93 | + |
| 94 | + NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder(); |
| 95 | + |
| 96 | + nativeSearchQueryBuilder.withPageable(PageRequest.of(0, 3)); |
| 97 | + nativeSearchQueryBuilder.withSorts(SortBuilders.fieldSort("id").order(SortOrder.ASC)); |
| 98 | + |
| 99 | + List<Object> searchAfter = null; |
| 100 | + List<Entity> foundEntities = new ArrayList<>(); |
| 101 | + |
| 102 | + int loop = 0; |
| 103 | + do { |
| 104 | + nativeSearchQueryBuilder.withSearchAfter(searchAfter); |
| 105 | + SearchHits<Entity> searchHits = operations.search(nativeSearchQueryBuilder.build(), Entity.class); |
| 106 | + |
| 107 | + if (searchHits.getSearchHits().size() == 0) { |
| 108 | + break; |
| 109 | + } |
| 110 | + foundEntities.addAll(searchHits.stream().map(searchHit -> searchHit.getContent()).collect(Collectors.toList())); |
| 111 | + searchAfter = searchHits.getSearchHit((int) (searchHits.getSearchHits().size() - 1)).getSortValues(); |
| 112 | + |
| 113 | + if (++loop > 10) { |
| 114 | + fail("loop not terminating"); |
| 115 | + } |
| 116 | + } while (true); |
| 117 | + |
| 118 | + assertThat(foundEntities).containsExactlyElementsOf(entities); |
| 119 | + } |
| 120 | + |
82 | 121 | @Document(indexName = "test-search-after")
|
83 | 122 | private static class Entity {
|
84 | 123 | @Nullable
|
|
0 commit comments