Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IndexOrDocValuesQuery to GeoPolygonQueryBuilder #48449

Merged
merged 1 commit into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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