mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-04 09:42:25 +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
@ -1492,6 +1492,7 @@ class RequestConverter {
|
|||||||
.field(fieldName) //
|
.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)) //
|
.distanceType(geoDistanceType(geoDistanceOrder.getDistanceType())).mode(sortMode(finalMode)) //
|
||||||
|
.order(sortOrder(geoDistanceOrder.getDirection())) //
|
||||||
.unit(distanceUnit(geoDistanceOrder.getUnit())) //
|
.unit(distanceUnit(geoDistanceOrder.getUnit())) //
|
||||||
.ignoreUnmapped(geoDistanceOrder.getIgnoreUnmapped())));
|
.ignoreUnmapped(geoDistanceOrder.getIgnoreUnmapped())));
|
||||||
} else {
|
} else {
|
||||||
|
@ -28,6 +28,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.data.elasticsearch.core.RefreshPolicy;
|
import org.springframework.data.elasticsearch.core.RefreshPolicy;
|
||||||
import org.springframework.data.elasticsearch.core.document.Document;
|
import org.springframework.data.elasticsearch.core.document.Document;
|
||||||
import org.springframework.data.elasticsearch.core.query.*;
|
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
|
@Nullable
|
||||||
static HighlighterFragmenter highlighterFragmenter(@Nullable String value) {
|
static HighlighterFragmenter highlighterFragmenter(@Nullable String value) {
|
||||||
|
|
||||||
|
@ -526,7 +526,8 @@ class RequestFactory {
|
|||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region delete
|
// 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);
|
SearchRequest searchRequest = searchRequest(query, routing, clazz, index);
|
||||||
DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(index.getIndexNames()) //
|
DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(index.getIndexNames()) //
|
||||||
.setQuery(searchRequest.source().query()) //
|
.setQuery(searchRequest.source().query()) //
|
||||||
@ -754,7 +755,8 @@ class RequestFactory {
|
|||||||
return searchRequest;
|
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);
|
elasticsearchConverter.updateQuery(query, clazz);
|
||||||
SearchRequest searchRequest = prepareSearchRequest(query, routing, clazz, index);
|
SearchRequest searchRequest = prepareSearchRequest(query, routing, clazz, index);
|
||||||
@ -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();
|
String[] indexNames = indexCoordinates.getIndexNames();
|
||||||
Assert.notNull(indexNames, "No index defined for Query");
|
Assert.notNull(indexNames, "No index defined for Query");
|
||||||
@ -968,6 +971,8 @@ class RequestFactory {
|
|||||||
sort.ignoreUnmapped(geoDistanceOrder.getIgnoreUnmapped());
|
sort.ignoreUnmapped(geoDistanceOrder.getIgnoreUnmapped());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sort.order(order.isAscending() ? SortOrder.ASC : SortOrder.DESC);
|
||||||
|
|
||||||
return sort;
|
return sort;
|
||||||
} else {
|
} else {
|
||||||
FieldSortBuilder sort = SortBuilders //
|
FieldSortBuilder sort = SortBuilders //
|
||||||
|
@ -1590,6 +1590,41 @@ public abstract class CustomMethodRepositoryIntegrationTests implements NewElast
|
|||||||
assertThat(searchHits.getSearchHit(2).getId()).isEqualTo("oslo");
|
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
|
@Test // DATAES-749
|
||||||
void shouldReturnSearchPage() {
|
void shouldReturnSearchPage() {
|
||||||
List<SampleEntity> entities = createSampleEntities("abc", 20);
|
List<SampleEntity> entities = createSampleEntities("abc", 20);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user