|
42 | 42 | import org.opensearch.common.settings.Settings; |
43 | 43 | import org.opensearch.common.unit.DistanceUnit; |
44 | 44 | import org.opensearch.core.xcontent.XContentBuilder; |
| 45 | +import org.opensearch.index.query.BoolQueryBuilder; |
45 | 46 | import org.opensearch.index.query.GeoValidationMethod; |
46 | 47 | import org.opensearch.search.builder.SearchSourceBuilder; |
47 | 48 | import org.opensearch.test.ParameterizedStaticSettingsOpenSearchIntegTestCase; |
|
56 | 57 | import java.util.concurrent.ExecutionException; |
57 | 58 |
|
58 | 59 | import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder; |
| 60 | +import static org.opensearch.index.query.QueryBuilders.boolQuery; |
| 61 | +import static org.opensearch.index.query.QueryBuilders.existsQuery; |
59 | 62 | import static org.opensearch.index.query.QueryBuilders.matchAllQuery; |
60 | 63 | import static org.opensearch.search.SearchService.CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING; |
61 | 64 | import static org.opensearch.search.sort.SortBuilders.fieldSort; |
@@ -430,4 +433,38 @@ public void testCrossIndexIgnoreUnmapped() throws Exception { |
430 | 433 | new Object[] { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY } |
431 | 434 | ); |
432 | 435 | } |
| 436 | + |
| 437 | + public void testGeoDistanceQueryThenSort() throws Exception { |
| 438 | + assertAcked(prepareCreate("index").setMapping("admin", "type=keyword", LOCATION_FIELD, "type=geo_point")); |
| 439 | + |
| 440 | + indexRandom( |
| 441 | + true, |
| 442 | + client().prepareIndex("index") |
| 443 | + .setId("d1") |
| 444 | + .setSource( |
| 445 | + jsonBuilder().startObject() |
| 446 | + .startObject(LOCATION_FIELD) |
| 447 | + .field("lat", 48.8331) |
| 448 | + .field("lon", 2.3264) |
| 449 | + .endObject() |
| 450 | + .field("admin", "11") |
| 451 | + .endObject() |
| 452 | + ) |
| 453 | + ); |
| 454 | + |
| 455 | + GeoDistanceSortBuilder geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, new GeoPoint(40.7128, -74.0060)); |
| 456 | + |
| 457 | + BoolQueryBuilder bool = boolQuery().filter(existsQuery(LOCATION_FIELD)); |
| 458 | + |
| 459 | + SearchResponse searchResponse = client().prepareSearch() |
| 460 | + .setQuery(bool) |
| 461 | + .addSort(geoDistanceSortBuilder.unit(DistanceUnit.KILOMETERS).ignoreUnmapped(true).order(SortOrder.DESC)) |
| 462 | + .setSize(4) |
| 463 | + .get(); |
| 464 | + assertOrderedSearchHits(searchResponse, "d1"); |
| 465 | + assertThat( |
| 466 | + (Double) searchResponse.getHits().getAt(0).getSortValues()[0], |
| 467 | + closeTo(GeoDistance.ARC.calculate(40.7128, -74.0060, 48.8331, 2.3264, DistanceUnit.KILOMETERS), 1.e-1) |
| 468 | + ); |
| 469 | + } |
433 | 470 | } |
0 commit comments