|
15 | 15 | */
|
16 | 16 | package org.springframework.data.elasticsearch.repository.support;
|
17 | 17 |
|
| 18 | +import co.elastic.clients.elasticsearch.core.search.FieldCollapse; |
| 19 | +import org.junit.jupiter.api.DisplayName; |
| 20 | +import org.junit.jupiter.api.Test; |
18 | 21 | import org.springframework.context.annotation.Bean;
|
19 | 22 | import org.springframework.context.annotation.Configuration;
|
20 | 23 | import org.springframework.context.annotation.Import;
|
| 24 | +import org.springframework.data.domain.Pageable; |
| 25 | +import org.springframework.data.domain.Sort; |
| 26 | +import org.springframework.data.elasticsearch.client.elc.NativeQuery; |
| 27 | +import org.springframework.data.elasticsearch.client.elc.Queries; |
21 | 28 | import org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchTemplateConfiguration;
|
22 | 29 | import org.springframework.data.elasticsearch.repositories.custommethod.QueryParameter;
|
23 | 30 | import org.springframework.data.elasticsearch.repository.config.EnableReactiveElasticsearchRepositories;
|
24 | 31 | import org.springframework.data.elasticsearch.utils.IndexNameProvider;
|
25 | 32 | import org.springframework.test.context.ContextConfiguration;
|
| 33 | +import reactor.test.StepVerifier; |
26 | 34 |
|
27 | 35 | /**
|
28 | 36 | * @author Peter-Josef Meisch
|
@@ -51,4 +59,33 @@ QueryParameter queryParameter() {
|
51 | 59 | }
|
52 | 60 | }
|
53 | 61 |
|
| 62 | + /** |
| 63 | + * search_after is used by the reactive search operation, it normally always adds _shard_doc as a tiebreaker sort |
| 64 | + * parameter. This must not be done when a collapse field is used as sort field, as in that case the collapse field |
| 65 | + * must be the only sort field. |
| 66 | + */ |
| 67 | + @Test // #2935 |
| 68 | + @DisplayName("should use collapse_field for search_after in pit search") |
| 69 | + void shouldUseCollapseFieldForSearchAfterI() { |
| 70 | + var entity = new SampleEntity(); |
| 71 | + entity.setId("42"); |
| 72 | + entity.setMessage("m"); |
| 73 | + entity.setKeyword("kw"); |
| 74 | + repository.save(entity).block(); |
| 75 | + |
| 76 | + var query = NativeQuery.builder() |
| 77 | + .withQuery(Queries.matchAllQueryAsQuery()) |
| 78 | + .withPageable(Pageable.unpaged()) |
| 79 | + .withFieldCollapse(FieldCollapse.of(fcb -> fcb |
| 80 | + .field("keyword"))) |
| 81 | + .withSort(Sort.by("keyword")) |
| 82 | + .build(); |
| 83 | + |
| 84 | + operations.search(query, SampleEntity.class) |
| 85 | + .as(StepVerifier::create) |
| 86 | + .expectNextCount(1) |
| 87 | + .verifyComplete(); |
| 88 | + } |
| 89 | + |
| 90 | + |
54 | 91 | }
|
0 commit comments