mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-04 01:32:11 +00:00
Adding GeoDistanceOrder's direction in request.
Original Pull Request #2602 Closes #2601 (cherry picked from commit 8a164b103937d6911f388ecd106996578119a599) Polishing (cherry picked from commit b7570ffa957254d114e161cf73e173135fb3ee54) add implementation for old client
This commit is contained in:
parent
a715d2836d
commit
d43b44ba9c
@ -1490,8 +1490,9 @@ class RequestConverter {
|
||||
return SortOptions.of(so -> so //
|
||||
.geoDistance(gd -> gd //
|
||||
.field(fieldName) //
|
||||
.location(loc -> loc.latlon(Queries.latLon(geoDistanceOrder.getGeoPoint())))//
|
||||
.location(loc -> loc.latlon(Queries.latLon(geoDistanceOrder.getGeoPoint()))) //
|
||||
.distanceType(geoDistanceType(geoDistanceOrder.getDistanceType())).mode(sortMode(finalMode)) //
|
||||
.order(sortOrder(geoDistanceOrder.getDirection())) //
|
||||
.unit(distanceUnit(geoDistanceOrder.getUnit())) //
|
||||
.ignoreUnmapped(geoDistanceOrder.getIgnoreUnmapped())));
|
||||
} else {
|
||||
|
@ -28,6 +28,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.elasticsearch.core.RefreshPolicy;
|
||||
import org.springframework.data.elasticsearch.core.document.Document;
|
||||
import org.springframework.data.elasticsearch.core.query.*;
|
||||
@ -165,6 +166,20 @@ final class TypeUtils {
|
||||
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static SortOrder sortOrder(@Nullable Sort.Direction direction) {
|
||||
|
||||
if (direction == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return switch (direction) {
|
||||
case ASC -> SortOrder.Asc;
|
||||
case DESC -> SortOrder.Desc;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static HighlighterFragmenter highlighterFragmenter(@Nullable String value) {
|
||||
|
||||
|
@ -348,7 +348,7 @@ class RequestFactory {
|
||||
for (String aliasName : parametersAliases) {
|
||||
Alias alias = new Alias(aliasName);
|
||||
|
||||
//noinspection DuplicatedCode
|
||||
// noinspection DuplicatedCode
|
||||
if (parameters.getRouting() != null) {
|
||||
alias.routing(parameters.getRouting());
|
||||
}
|
||||
@ -526,7 +526,8 @@ class RequestFactory {
|
||||
// endregion
|
||||
|
||||
// region delete
|
||||
public DeleteByQueryRequest deleteByQueryRequest(Query query, @Nullable String routing, Class<?> clazz, IndexCoordinates index) {
|
||||
public DeleteByQueryRequest deleteByQueryRequest(Query query, @Nullable String routing, Class<?> clazz,
|
||||
IndexCoordinates index) {
|
||||
SearchRequest searchRequest = searchRequest(query, routing, clazz, index);
|
||||
DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(index.getIndexNames()) //
|
||||
.setQuery(searchRequest.source().query()) //
|
||||
@ -754,10 +755,11 @@ class RequestFactory {
|
||||
return searchRequest;
|
||||
}
|
||||
|
||||
public SearchRequest searchRequest(Query query, @Nullable String routing, @Nullable Class<?> clazz, IndexCoordinates index) {
|
||||
public SearchRequest searchRequest(Query query, @Nullable String routing, @Nullable Class<?> clazz,
|
||||
IndexCoordinates index) {
|
||||
|
||||
elasticsearchConverter.updateQuery(query, clazz);
|
||||
SearchRequest searchRequest = prepareSearchRequest(query, routing,clazz, index);
|
||||
SearchRequest searchRequest = prepareSearchRequest(query, routing, clazz, index);
|
||||
QueryBuilder elasticsearchQuery = getQuery(query);
|
||||
QueryBuilder elasticsearchFilter = getFilter(query);
|
||||
|
||||
@ -771,7 +773,8 @@ class RequestFactory {
|
||||
|
||||
}
|
||||
|
||||
private SearchRequest prepareSearchRequest(Query query, @Nullable String routing, @Nullable Class<?> clazz, IndexCoordinates indexCoordinates) {
|
||||
private SearchRequest prepareSearchRequest(Query query, @Nullable String routing, @Nullable Class<?> clazz,
|
||||
IndexCoordinates indexCoordinates) {
|
||||
|
||||
String[] indexNames = indexCoordinates.getIndexNames();
|
||||
Assert.notNull(indexNames, "No index defined for Query");
|
||||
@ -968,6 +971,8 @@ class RequestFactory {
|
||||
sort.ignoreUnmapped(geoDistanceOrder.getIgnoreUnmapped());
|
||||
}
|
||||
|
||||
sort.order(order.isAscending() ? SortOrder.ASC : SortOrder.DESC);
|
||||
|
||||
return sort;
|
||||
} else {
|
||||
FieldSortBuilder sort = SortBuilders //
|
||||
|
@ -1590,6 +1590,41 @@ public abstract class CustomMethodRepositoryIntegrationTests implements NewElast
|
||||
assertThat(searchHits.getSearchHit(2).getId()).isEqualTo("oslo");
|
||||
}
|
||||
|
||||
@Test // #2601
|
||||
void shouldUseGeoSortReverseParameter() {
|
||||
GeoPoint munich = new GeoPoint(48.137154, 11.5761247);
|
||||
GeoPoint berlin = new GeoPoint(52.520008, 13.404954);
|
||||
GeoPoint vienna = new GeoPoint(48.20849, 16.37208);
|
||||
GeoPoint oslo = new GeoPoint(59.9127, 10.7461);
|
||||
|
||||
List<SampleEntity> entities = new ArrayList<>();
|
||||
|
||||
SampleEntity entity1 = new SampleEntity();
|
||||
entity1.setId("berlin");
|
||||
entity1.setLocation(berlin);
|
||||
entities.add(entity1);
|
||||
|
||||
SampleEntity entity2 = new SampleEntity();
|
||||
entity2.setId("vienna");
|
||||
entity2.setLocation(vienna);
|
||||
entities.add(entity2);
|
||||
|
||||
SampleEntity entity3 = new SampleEntity();
|
||||
entity3.setId("oslo");
|
||||
entity3.setLocation(oslo);
|
||||
entities.add(entity3);
|
||||
|
||||
repository.saveAll(entities);
|
||||
|
||||
SearchHits<SampleEntity> searchHits = repository
|
||||
.searchBy(Sort.by(new GeoDistanceOrder("location", munich).with(Sort.Direction.DESC)));
|
||||
|
||||
assertThat(searchHits.getTotalHits()).isEqualTo(3);
|
||||
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo("oslo");
|
||||
assertThat(searchHits.getSearchHit(1).getId()).isEqualTo("berlin");
|
||||
assertThat(searchHits.getSearchHit(2).getId()).isEqualTo("vienna");
|
||||
}
|
||||
|
||||
@Test // DATAES-749
|
||||
void shouldReturnSearchPage() {
|
||||
List<SampleEntity> entities = createSampleEntities("abc", 20);
|
||||
|
Loading…
x
Reference in New Issue
Block a user