|
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; |
| 47 | +import org.opensearch.index.query.MatchAllQueryBuilder; |
46 | 48 | import org.opensearch.search.builder.SearchSourceBuilder; |
47 | 49 | import org.opensearch.test.ParameterizedStaticSettingsOpenSearchIntegTestCase; |
48 | 50 | import org.opensearch.test.VersionUtils; |
|
56 | 58 | import java.util.concurrent.ExecutionException; |
57 | 59 |
|
58 | 60 | import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder; |
| 61 | +import static org.opensearch.index.query.QueryBuilders.boolQuery; |
| 62 | +import static org.opensearch.index.query.QueryBuilders.existsQuery; |
59 | 63 | import static org.opensearch.index.query.QueryBuilders.matchAllQuery; |
60 | 64 | import static org.opensearch.search.SearchService.CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING; |
61 | 65 | import static org.opensearch.search.sort.SortBuilders.fieldSort; |
@@ -430,4 +434,56 @@ public void testCrossIndexIgnoreUnmapped() throws Exception { |
430 | 434 | new Object[] { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY } |
431 | 435 | ); |
432 | 436 | } |
| 437 | + |
| 438 | + public void testGeoDistanceQueryThenSort() throws Exception { |
| 439 | + assertAcked(prepareCreate("index").setMapping("admin", "type=keyword", LOCATION_FIELD, "type=geo_point")); |
| 440 | + |
| 441 | + indexRandom( |
| 442 | + true, |
| 443 | + client().prepareIndex("index") |
| 444 | + .setId("d1") |
| 445 | + .setSource( |
| 446 | + jsonBuilder().startObject() |
| 447 | + .startObject(LOCATION_FIELD) |
| 448 | + .field("lat", 48.8331) |
| 449 | + .field("lon", 2.3264) |
| 450 | + .endObject() |
| 451 | + .field("admin", "11") |
| 452 | + .endObject() |
| 453 | + ) |
| 454 | + ); |
| 455 | + |
| 456 | + GeoDistanceSortBuilder geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, new GeoPoint(40.7128, -74.0060)); |
| 457 | + |
| 458 | + BoolQueryBuilder bool = boolQuery().filter(existsQuery(LOCATION_FIELD)); |
| 459 | + |
| 460 | + SearchResponse searchResponse = client().prepareSearch() |
| 461 | + .setQuery(bool) |
| 462 | + .addSort(geoDistanceSortBuilder.unit(DistanceUnit.KILOMETERS).ignoreUnmapped(true).order(SortOrder.DESC)) |
| 463 | + .setSize(4) |
| 464 | + .get(); |
| 465 | + assertOrderedSearchHits(searchResponse, "d1"); |
| 466 | + assertThat( |
| 467 | + (Double) searchResponse.getHits().getAt(0).getSortValues()[0], |
| 468 | + closeTo(GeoDistance.ARC.calculate(40.7128, -74.0060, 48.8331, 2.3264, DistanceUnit.KILOMETERS), 1.e-1) |
| 469 | + ); |
| 470 | + |
| 471 | + geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, new GeoPoint(9.227400, 49.189800)); |
| 472 | + searchResponse = client().prepareSearch() |
| 473 | + .setQuery(new MatchAllQueryBuilder()) |
| 474 | + .addSort( |
| 475 | + geoDistanceSortBuilder.unit(DistanceUnit.KILOMETERS) |
| 476 | + .ignoreUnmapped(true) |
| 477 | + .order(SortOrder.DESC) |
| 478 | + .geoDistance(GeoDistance.ARC) |
| 479 | + .sortMode(SortMode.MIN) |
| 480 | + ) |
| 481 | + .setSize(10) |
| 482 | + .get(); |
| 483 | + assertOrderedSearchHits(searchResponse, "d1"); |
| 484 | + assertThat( |
| 485 | + (Double) searchResponse.getHits().getAt(0).getSortValues()[0], |
| 486 | + closeTo(GeoDistance.ARC.calculate(9.227400, 49.189800, 48.8331, 2.3264, DistanceUnit.KILOMETERS), 1.e-1) |
| 487 | + ); |
| 488 | + } |
433 | 489 | } |
0 commit comments