Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
setup:
- do:
indices.create:
index: index
body:
mappings:
properties:
admin:
type: keyword
location:
type: geo_point
- do:
index:
index: index
refresh: true
body: {admin: "11", location: {lat: 48.8331, lon: 2.3264}}

---
"test query then sort with geo_point distance":

- do:
search:
index: index
body:
query:
exists:
field: location
sort: [{_geo_distance: {location: {lat: 40.7128, lon: -74.0060}, ignore_unmapped: true, order: "desc", unit: "km"}}]
size: 4
- match: { hits.total.value: 1 }

- do:
search:
index: index
body:
query: { match_all: {} }
sort: [{_geo_distance: {location: [9.227400, 49.189800], order: "desc", unit: "km", distance_type: "arc", mode: "min", ignore_unmapped: true } } ]
size: 10
- match: { hits.total.value: 1 }
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.DistanceUnit;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.index.query.BoolQueryBuilder;
import org.opensearch.index.query.GeoValidationMethod;
import org.opensearch.index.query.MatchAllQueryBuilder;
import org.opensearch.search.builder.SearchSourceBuilder;
import org.opensearch.test.ParameterizedStaticSettingsOpenSearchIntegTestCase;
import org.opensearch.test.VersionUtils;
Expand All @@ -56,6 +58,8 @@
import java.util.concurrent.ExecutionException;

import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.opensearch.index.query.QueryBuilders.boolQuery;
import static org.opensearch.index.query.QueryBuilders.existsQuery;
import static org.opensearch.index.query.QueryBuilders.matchAllQuery;
import static org.opensearch.search.SearchService.CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING;
import static org.opensearch.search.sort.SortBuilders.fieldSort;
Expand Down Expand Up @@ -430,4 +434,56 @@ public void testCrossIndexIgnoreUnmapped() throws Exception {
new Object[] { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY }
);
}

public void testGeoDistanceQueryThenSort() throws Exception {
assertAcked(prepareCreate("index").setMapping("admin", "type=keyword", LOCATION_FIELD, "type=geo_point"));

indexRandom(
true,
client().prepareIndex("index")
.setId("d1")
.setSource(
jsonBuilder().startObject()
.startObject(LOCATION_FIELD)
.field("lat", 48.8331)
.field("lon", 2.3264)
.endObject()
.field("admin", "11")
.endObject()
)
);

GeoDistanceSortBuilder geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, new GeoPoint(40.7128, -74.0060));

BoolQueryBuilder bool = boolQuery().filter(existsQuery(LOCATION_FIELD));

SearchResponse searchResponse = client().prepareSearch()
.setQuery(bool)
.addSort(geoDistanceSortBuilder.unit(DistanceUnit.KILOMETERS).ignoreUnmapped(true).order(SortOrder.DESC))
.setSize(4)
.get();
assertOrderedSearchHits(searchResponse, "d1");
assertThat(
(Double) searchResponse.getHits().getAt(0).getSortValues()[0],
closeTo(GeoDistance.ARC.calculate(40.7128, -74.0060, 48.8331, 2.3264, DistanceUnit.KILOMETERS), 1.e-1)
);

geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, new GeoPoint(9.227400, 49.189800));
searchResponse = client().prepareSearch()
.setQuery(new MatchAllQueryBuilder())
.addSort(
geoDistanceSortBuilder.unit(DistanceUnit.KILOMETERS)
.ignoreUnmapped(true)
.order(SortOrder.DESC)
.geoDistance(GeoDistance.ARC)
.sortMode(SortMode.MIN)
)
.setSize(10)
.get();
assertOrderedSearchHits(searchResponse, "d1");
assertThat(
(Double) searchResponse.getHits().getAt(0).getSortValues()[0],
closeTo(GeoDistance.ARC.calculate(9.227400, 49.189800, 48.8331, 2.3264, DistanceUnit.KILOMETERS), 1.e-1)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.automaton.CompiledAutomaton;
import org.opensearch.common.lucene.index.SequentialStoredFieldsLeafReader;
import org.opensearch.core.common.Strings;

import java.io.IOException;

Expand Down Expand Up @@ -108,6 +109,9 @@ private ExitableLeafReader(LeafReader leafReader, QueryCancellation queryCancell

@Override
public PointValues getPointValues(String field) throws IOException {
if (Strings.isEmpty(field)) {
return null;
}
final PointValues pointValues = in.getPointValues(field);
if (pointValues == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public void testExitableDirectoryReader() throws IOException {
);

cancelled.set(false); // Avoid exception during construction of the wrapper objects
assertNull(searcher.getIndexReader().leaves().get(0).reader().getPointValues(null));
Terms terms = searcher.getIndexReader().leaves().get(0).reader().terms(STRING_FIELD_NAME);
TermsEnum termsIterator = terms.iterator();
TermsEnum termsIntersect = terms.intersect(automaton, null);
Expand Down
Loading