Skip to content

Commit

Permalink
Add IndexOrDocValuesQuery to GeoPolygonQueryBuilder (#48449)
Browse files Browse the repository at this point in the history
  • Loading branch information
iverase committed Oct 31, 2019
1 parent 0533b44 commit 5251088
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

package org.elasticsearch.index.query;

import org.apache.lucene.document.LatLonDocValuesField;
import org.apache.lucene.document.LatLonPoint;
import org.apache.lucene.geo.Polygon;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.ParseField;
Expand Down Expand Up @@ -199,7 +201,13 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
lons[i] = p.lon();
}

return LatLonPoint.newPolygonQuery(fieldType.name(), new Polygon(lats, lons));
Polygon polygon = new Polygon(lats, lons);
Query query = LatLonPoint.newPolygonQuery(fieldType.name(), polygon);
if (fieldType.hasDocValues()) {
Query dvQuery = LatLonDocValuesField.newSlowPolygonQuery(fieldType.name(), polygon);
query = new IndexOrDocValuesQuery(query, dvQuery);
}
return query;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@

package org.elasticsearch.index.query;

import org.apache.lucene.document.LatLonDocValuesField;
import org.apache.lucene.document.LatLonPoint;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.test.AbstractQueryTestCase;
import org.elasticsearch.test.geo.RandomShapeGenerator;
import org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType;
Expand Down Expand Up @@ -53,12 +57,30 @@ protected GeoPolygonQueryBuilder doCreateTestQueryBuilder() {
if (randomBoolean()) {
builder.ignoreUnmapped(randomBoolean());
}

return builder;
}

@Override
protected void doAssertLuceneQuery(GeoPolygonQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
// todo LatLonPointInPolygon is package private
MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
if (fieldType == null) {
assertTrue("Found no indexed geo query.", query instanceof MatchNoDocsQuery);
} else { // TODO: Test case when there are no docValues
Query indexQuery = ((IndexOrDocValuesQuery) query).getIndexQuery();
String expectedFieldName = expectedFieldName(queryBuilder.fieldName());
List<GeoPoint> points = queryBuilder.points();
double[] lats = new double[points.size()];
double[] lons = new double[points.size()];
for (int i =0; i < points.size(); i++) {
lats[i] = points.get(i).getLat();
lons[i] = points.get(i).getLon();
}
org.apache.lucene.geo.Polygon polygon = new org.apache.lucene.geo.Polygon(lats, lons);
assertEquals(LatLonPoint.newPolygonQuery(expectedFieldName, polygon), indexQuery);
Query dvQuery = ((IndexOrDocValuesQuery) query).getRandomAccessQuery();
assertEquals(LatLonDocValuesField.newSlowPolygonQuery(expectedFieldName, polygon), dvQuery);
}
}

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

private void assertGeoPolygonQuery(String query) throws IOException {
QueryShardContext context = createShardContext();
parseQuery(query).toQuery(context);
// TODO LatLonPointInPolygon is package private, need a closeTo check on the query
// since some points can be computed from the geohash
GeoPolygonQueryBuilder queryBuilder = (GeoPolygonQueryBuilder) parseQuery(query);
doAssertLuceneQuery(queryBuilder, queryBuilder.toQuery(context), context);
}

public void testFromJson() throws IOException {
Expand Down

0 comments on commit 5251088

Please sign in to comment.