Skip to content

Commit 5bce5dd

Browse files
beyond-seunghyunsothawo
authored andcommitted
Adding GeoDistanceOrder's direction in request.
Original Pull Request #2602 Closes #2601 (cherry picked from commit 8a164b1) Polishing (cherry picked from commit b7570ff) add implementation for old client (cherry picked from commit d43b44b) # Conflicts: # src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java # src/main/java/org/springframework/data/elasticsearch/client/erhlc/RequestFactory.java
1 parent 32c4c4d commit 5bce5dd

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1373,9 +1373,10 @@ private SortOptions getSortOptions(Sort.Order order, @Nullable ElasticsearchPers
13731373
return SortOptions.of(so -> so //
13741374
.geoDistance(gd -> gd //
13751375
.field(fieldName) //
1376-
.location(loc -> loc.latlon(QueryBuilders.latLon(geoDistanceOrder.getGeoPoint())))//
1376+
.location(loc -> loc.latlon(QueryBuilders.latLon(geoDistanceOrder.getGeoPoint()))) //
13771377
.distanceType(TypeUtils.geoDistanceType(geoDistanceOrder.getDistanceType()))
13781378
.mode(TypeUtils.sortMode(finalMode)) //
1379+
.order(TypeUtils.sortOrder(geoDistanceOrder.getDirection())) //
13791380
.unit(TypeUtils.distanceUnit(geoDistanceOrder.getUnit())) //
13801381
.ignoreUnmapped(geoDistanceOrder.getIgnoreUnmapped())));
13811382
} else {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.time.Duration;
2929

30+
import org.springframework.data.domain.Sort;
3031
import org.springframework.data.elasticsearch.core.RefreshPolicy;
3132
import org.springframework.data.elasticsearch.core.query.GeoDistanceOrder;
3233
import org.springframework.data.elasticsearch.core.query.IndexQuery;
@@ -136,6 +137,20 @@ static GeoDistanceType geoDistanceType(GeoDistanceOrder.DistanceType distanceTyp
136137

137138
}
138139

140+
@Nullable
141+
static SortOrder sortOrder(@Nullable Sort.Direction direction) {
142+
143+
if (direction == null) {
144+
return null;
145+
}
146+
147+
return switch (direction) {
148+
case ASC -> SortOrder.Asc;
149+
case DESC -> SortOrder.Desc;
150+
};
151+
152+
}
153+
139154
@Nullable
140155
static HighlighterFragmenter highlighterFragmenter(@Nullable String value) {
141156

src/main/java/org/springframework/data/elasticsearch/client/erhlc/RequestFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,8 @@ private SortBuilder<?> getSortBuilder(Sort.Order order, @Nullable ElasticsearchP
963963
sort.ignoreUnmapped(geoDistanceOrder.getIgnoreUnmapped());
964964
}
965965

966+
sort.order(order.isAscending() ? SortOrder.ASC : SortOrder.DESC);
967+
966968
return sort;
967969
} else {
968970
FieldSortBuilder sort = SortBuilders //

src/test/java/org/springframework/data/elasticsearch/repositories/custommethod/CustomMethodRepositoryIntegrationTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,41 @@ void shouldUseGeoSortParameter() {
15901590
assertThat(searchHits.getSearchHit(2).getId()).isEqualTo("oslo");
15911591
}
15921592

1593+
@Test // #2601
1594+
void shouldUseGeoSortReverseParameter() {
1595+
GeoPoint munich = new GeoPoint(48.137154, 11.5761247);
1596+
GeoPoint berlin = new GeoPoint(52.520008, 13.404954);
1597+
GeoPoint vienna = new GeoPoint(48.20849, 16.37208);
1598+
GeoPoint oslo = new GeoPoint(59.9127, 10.7461);
1599+
1600+
List<SampleEntity> entities = new ArrayList<>();
1601+
1602+
SampleEntity entity1 = new SampleEntity();
1603+
entity1.setId("berlin");
1604+
entity1.setLocation(berlin);
1605+
entities.add(entity1);
1606+
1607+
SampleEntity entity2 = new SampleEntity();
1608+
entity2.setId("vienna");
1609+
entity2.setLocation(vienna);
1610+
entities.add(entity2);
1611+
1612+
SampleEntity entity3 = new SampleEntity();
1613+
entity3.setId("oslo");
1614+
entity3.setLocation(oslo);
1615+
entities.add(entity3);
1616+
1617+
repository.saveAll(entities);
1618+
1619+
SearchHits<SampleEntity> searchHits = repository
1620+
.searchBy(Sort.by(new GeoDistanceOrder("location", munich).with(Sort.Direction.DESC)));
1621+
1622+
assertThat(searchHits.getTotalHits()).isEqualTo(3);
1623+
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo("oslo");
1624+
assertThat(searchHits.getSearchHit(1).getId()).isEqualTo("berlin");
1625+
assertThat(searchHits.getSearchHit(2).getId()).isEqualTo("vienna");
1626+
}
1627+
15931628
@Test // DATAES-749
15941629
void shouldReturnSearchPage() {
15951630
List<SampleEntity> entities = createSampleEntities("abc", 20);

0 commit comments

Comments
 (0)