Skip to content

Commit 5bea389

Browse files
authored
Add IndexOrDocValuesQuery to GeoPolygonQueryBuilder (#48449) (#48731)
1 parent 51179f1 commit 5bea389

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

server/src/main/java/org/elasticsearch/index/query/GeoPolygonQueryBuilder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
package org.elasticsearch.index.query;
2121

22+
import org.apache.lucene.document.LatLonDocValuesField;
2223
import org.apache.lucene.document.LatLonPoint;
2324
import org.apache.lucene.geo.Polygon;
25+
import org.apache.lucene.search.IndexOrDocValuesQuery;
2426
import org.apache.lucene.search.MatchNoDocsQuery;
2527
import org.apache.lucene.search.Query;
2628
import org.elasticsearch.common.ParseField;
@@ -199,7 +201,13 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
199201
lons[i] = p.lon();
200202
}
201203

202-
return LatLonPoint.newPolygonQuery(fieldType.name(), new Polygon(lats, lons));
204+
Polygon polygon = new Polygon(lats, lons);
205+
Query query = LatLonPoint.newPolygonQuery(fieldType.name(), polygon);
206+
if (fieldType.hasDocValues()) {
207+
Query dvQuery = LatLonDocValuesField.newSlowPolygonQuery(fieldType.name(), polygon);
208+
query = new IndexOrDocValuesQuery(query, dvQuery);
209+
}
210+
return query;
203211
}
204212

205213
@Override

server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919

2020
package org.elasticsearch.index.query;
2121

22+
import org.apache.lucene.document.LatLonDocValuesField;
23+
import org.apache.lucene.document.LatLonPoint;
24+
import org.apache.lucene.search.IndexOrDocValuesQuery;
2225
import org.apache.lucene.search.MatchNoDocsQuery;
2326
import org.apache.lucene.search.Query;
2427
import org.elasticsearch.common.ParsingException;
2528
import org.elasticsearch.common.geo.GeoPoint;
2629
import org.elasticsearch.common.geo.builders.ShapeBuilder;
30+
import org.elasticsearch.index.mapper.MappedFieldType;
2731
import org.elasticsearch.test.AbstractQueryTestCase;
2832
import org.elasticsearch.test.geo.RandomShapeGenerator;
2933
import org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType;
@@ -53,12 +57,30 @@ protected GeoPolygonQueryBuilder doCreateTestQueryBuilder() {
5357
if (randomBoolean()) {
5458
builder.ignoreUnmapped(randomBoolean());
5559
}
60+
5661
return builder;
5762
}
5863

5964
@Override
6065
protected void doAssertLuceneQuery(GeoPolygonQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
61-
// todo LatLonPointInPolygon is package private
66+
MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
67+
if (fieldType == null) {
68+
assertTrue("Found no indexed geo query.", query instanceof MatchNoDocsQuery);
69+
} else { // TODO: Test case when there are no docValues
70+
Query indexQuery = ((IndexOrDocValuesQuery) query).getIndexQuery();
71+
String expectedFieldName = expectedFieldName(queryBuilder.fieldName());
72+
List<GeoPoint> points = queryBuilder.points();
73+
double[] lats = new double[points.size()];
74+
double[] lons = new double[points.size()];
75+
for (int i =0; i < points.size(); i++) {
76+
lats[i] = points.get(i).getLat();
77+
lons[i] = points.get(i).getLon();
78+
}
79+
org.apache.lucene.geo.Polygon polygon = new org.apache.lucene.geo.Polygon(lats, lons);
80+
assertEquals(LatLonPoint.newPolygonQuery(expectedFieldName, polygon), indexQuery);
81+
Query dvQuery = ((IndexOrDocValuesQuery) query).getRandomAccessQuery();
82+
assertEquals(LatLonDocValuesField.newSlowPolygonQuery(expectedFieldName, polygon), dvQuery);
83+
}
6284
}
6385

6486
private static List<GeoPoint> randomPolygon() {
@@ -196,9 +218,8 @@ public void testParsingAndToQuery4() throws IOException {
196218

197219
private void assertGeoPolygonQuery(String query) throws IOException {
198220
QueryShardContext context = createShardContext();
199-
parseQuery(query).toQuery(context);
200-
// TODO LatLonPointInPolygon is package private, need a closeTo check on the query
201-
// since some points can be computed from the geohash
221+
GeoPolygonQueryBuilder queryBuilder = (GeoPolygonQueryBuilder) parseQuery(query);
222+
doAssertLuceneQuery(queryBuilder, queryBuilder.toQuery(context), context);
202223
}
203224

204225
public void testFromJson() throws IOException {

0 commit comments

Comments
 (0)