Skip to content

Commit 44b0ef0

Browse files
committed
Fix reactive native sort.
Original Pull Request #2746 Closes #2745 (cherry picked from commit 73fe086) (cherry picked from commit 2c85717)
1 parent 69329bc commit 44b0ef0

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,13 @@ private <T> void prepareSearchRequest(Query query, @Nullable Class<T> clazz, Ind
11921192

11931193
builder.searchType(searchType(query.getSearchType()));
11941194

1195+
addHighlight(query, builder);
1196+
1197+
if (query instanceof NativeQuery) {
1198+
prepareNativeSearch((NativeQuery) query, builder);
1199+
}
1200+
// query.getSort() must be checked after prepareNativeSearch as this already might hav a sort set that must have
1201+
// higher priority
11951202
if (query.getSort() != null) {
11961203
List<SortOptions> sortOptions = getSortOptions(query.getSort(), persistentEntity);
11971204

@@ -1200,12 +1207,6 @@ private <T> void prepareSearchRequest(Query query, @Nullable Class<T> clazz, Ind
12001207
}
12011208
}
12021209

1203-
addHighlight(query, builder);
1204-
1205-
if (query instanceof NativeQuery) {
1206-
prepareNativeSearch((NativeQuery) query, builder);
1207-
}
1208-
12091210
if (query.getTrackTotalHits() != null) {
12101211
// logic from the RHLC, choose between -1 and Integer.MAX_VALUE
12111212
int value = query.getTrackTotalHits() ? Integer.MAX_VALUE : -1;

src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchELCIntegrationTests.java

+49
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
2020

21+
import co.elastic.clients.elasticsearch._types.SortOrder;
2122
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
2223
import co.elastic.clients.elasticsearch._types.aggregations.Buckets;
2324
import co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate;
@@ -27,10 +28,13 @@
2728
import java.util.List;
2829
import java.util.concurrent.atomic.AtomicInteger;
2930

31+
import org.junit.jupiter.api.DisplayName;
32+
import org.junit.jupiter.api.Test;
3033
import org.springframework.context.annotation.Bean;
3134
import org.springframework.context.annotation.Configuration;
3235
import org.springframework.context.annotation.Import;
3336
import org.springframework.data.elasticsearch.ELCQueries;
37+
import org.springframework.data.domain.Pageable;
3438
import org.springframework.data.elasticsearch.client.elc.Aggregation;
3539
import org.springframework.data.elasticsearch.client.elc.ElasticsearchAggregation;
3640
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
@@ -48,6 +52,51 @@
4852
@ContextConfiguration(classes = ReactiveElasticsearchELCIntegrationTests.Config.class)
4953
public class ReactiveElasticsearchELCIntegrationTests extends ReactiveElasticsearchIntegrationTests {
5054

55+
@Test // #2745
56+
@DisplayName("should use sort defined in native unbounded query")
57+
void shouldUseSortDefinedInNativeUnboundedQuery() {
58+
var entity1 = randomEntity(null);
59+
entity1.setRate(7);
60+
var entity2 = randomEntity(null);
61+
entity2.setRate(5);
62+
var entity3 = randomEntity(null);
63+
entity3.setRate(11);
64+
65+
operations.saveAll(List.of(entity1, entity2, entity3), SampleEntity.class).blockLast();
66+
67+
var query = NativeQuery.builder()
68+
.withQuery(qb -> qb
69+
.matchAll(m -> m))
70+
.withSort(sob -> sob
71+
.field(f -> f
72+
.field("rate")
73+
.order(SortOrder.Asc)))
74+
.withPageable(Pageable.unpaged())
75+
.build();
76+
77+
var rates = operations.search(query, SampleEntity.class)
78+
.map(SearchHit::getContent)
79+
.map(SampleEntity::getRate)
80+
.collectList().block();
81+
assertThat(rates).containsExactly(5, 7, 11);
82+
83+
query = NativeQuery.builder()
84+
.withQuery(qb -> qb
85+
.matchAll(m -> m))
86+
.withSort(sob -> sob
87+
.field(f -> f
88+
.field("rate")
89+
.order(SortOrder.Desc)))
90+
.withPageable(Pageable.unpaged())
91+
.build();
92+
93+
rates = operations.search(query, SampleEntity.class)
94+
.map(SearchHit::getContent)
95+
.map(SampleEntity::getRate)
96+
.collectList().block();
97+
assertThat(rates).containsExactly(11, 7, 5);
98+
}
99+
51100
@Configuration
52101
@Import({ ReactiveElasticsearchTemplateConfiguration.class })
53102
static class Config {

src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchIntegrationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
@SpringIntegrationTest
9797
public abstract class ReactiveElasticsearchIntegrationTests {
9898

99-
@Autowired private ReactiveElasticsearchOperations operations;
99+
@Autowired protected ReactiveElasticsearchOperations operations;
100100
@Autowired private IndexNameProvider indexNameProvider;
101101

102102
// region Setup
@@ -1165,7 +1165,7 @@ void shouldWorkWithReadonlyId() {
11651165
// endregion
11661166

11671167
// region Helper functions
1168-
private SampleEntity randomEntity(String message) {
1168+
protected SampleEntity randomEntity(@Nullable String message) {
11691169

11701170
SampleEntity entity = new SampleEntity();
11711171
entity.setId(UUID.randomUUID().toString());

0 commit comments

Comments
 (0)