|
19 | 19 |
|
20 | 20 | package org.elasticsearch.index.query;
|
21 | 21 |
|
| 22 | +import org.apache.lucene.document.LatLonDocValuesField; |
| 23 | +import org.apache.lucene.document.LatLonPoint; |
| 24 | +import org.apache.lucene.search.IndexOrDocValuesQuery; |
22 | 25 | import org.apache.lucene.search.MatchNoDocsQuery;
|
23 | 26 | import org.apache.lucene.search.Query;
|
24 | 27 | import org.elasticsearch.common.ParsingException;
|
25 | 28 | import org.elasticsearch.common.geo.GeoPoint;
|
26 | 29 | import org.elasticsearch.common.geo.builders.ShapeBuilder;
|
| 30 | +import org.elasticsearch.index.mapper.MappedFieldType; |
27 | 31 | import org.elasticsearch.test.AbstractQueryTestCase;
|
28 | 32 | import org.elasticsearch.test.geo.RandomShapeGenerator;
|
29 | 33 | import org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType;
|
@@ -53,12 +57,30 @@ protected GeoPolygonQueryBuilder doCreateTestQueryBuilder() {
|
53 | 57 | if (randomBoolean()) {
|
54 | 58 | builder.ignoreUnmapped(randomBoolean());
|
55 | 59 | }
|
| 60 | + |
56 | 61 | return builder;
|
57 | 62 | }
|
58 | 63 |
|
59 | 64 | @Override
|
60 | 65 | 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 | + } |
62 | 84 | }
|
63 | 85 |
|
64 | 86 | private static List<GeoPoint> randomPolygon() {
|
@@ -196,9 +218,8 @@ public void testParsingAndToQuery4() throws IOException {
|
196 | 218 |
|
197 | 219 | private void assertGeoPolygonQuery(String query) throws IOException {
|
198 | 220 | 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); |
202 | 223 | }
|
203 | 224 |
|
204 | 225 | public void testFromJson() throws IOException {
|
|
0 commit comments