Skip to content

Commit de82425

Browse files
committed
added tests for code coverage
Signed-off-by: Anthony Leong <aj.leong623@gmail.com>
1 parent c94c434 commit de82425

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

server/src/internalClusterTest/java/org/opensearch/search/sort/GeoDistanceSortBuilderIT.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.opensearch.common.settings.Settings;
4343
import org.opensearch.common.unit.DistanceUnit;
4444
import org.opensearch.core.xcontent.XContentBuilder;
45+
import org.opensearch.index.query.BoolQueryBuilder;
4546
import org.opensearch.index.query.GeoValidationMethod;
4647
import org.opensearch.search.builder.SearchSourceBuilder;
4748
import org.opensearch.test.ParameterizedStaticSettingsOpenSearchIntegTestCase;
@@ -56,6 +57,8 @@
5657
import java.util.concurrent.ExecutionException;
5758

5859
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;
5962
import static org.opensearch.index.query.QueryBuilders.matchAllQuery;
6063
import static org.opensearch.search.SearchService.CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING;
6164
import static org.opensearch.search.sort.SortBuilders.fieldSort;
@@ -430,4 +433,38 @@ public void testCrossIndexIgnoreUnmapped() throws Exception {
430433
new Object[] { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY }
431434
);
432435
}
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+
}
433470
}

0 commit comments

Comments
 (0)