Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-6904: rewrite GeoPointInBBoxQuery to FieldValueQuery when min/max lon/lat values are set to limits.

Nick Knizebc01212d1ae2
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/GeoPointField.java b/lucene/sandbox/src/java/org/apache/lucene/document/GeoPointField.java
index 2f07da8e4ec..dd87ad510bb 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/document/GeoPointField.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/document/GeoPointField.java
@@ -104,6 +104,9 @@ public final class GeoPointField extends Field {
if (type.numericType() != FieldType.NumericType.LONG) {
throw new IllegalArgumentException("type.numericType() must be LONG but got " + type.numericType());
}
+ if (type.docValuesType() != DocValuesType.SORTED_NUMERIC) {
+ throw new IllegalArgumentException("type.docValuesType() must be SORTED_NUMERIC but got " + type.docValuesType());
+ }
fieldsData = GeoUtils.mortonHash(lon, lat);
}
diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInBBoxQuery.java b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInBBoxQuery.java
index e776a491939..54f85f47157 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInBBoxQuery.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInBBoxQuery.java
@@ -18,6 +18,7 @@ package org.apache.lucene.search;
*/
import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.util.GeoUtils;
/** Implements a simple bounding box query on a GeoPoint field. This is inspired by
* {@link org.apache.lucene.search.NumericRangeQuery} and is implemented using a
@@ -55,6 +56,13 @@ public class GeoPointInBBoxQuery extends Query {
@Override
public Query rewrite(IndexReader reader) {
+ // short-circuit to match all if specifying the whole map
+ if (minLon == GeoUtils.MIN_LON_INCL && maxLon == GeoUtils.MAX_LON_INCL
+ && minLat == GeoUtils.MIN_LAT_INCL && maxLat == GeoUtils.MAX_LAT_INCL) {
+ // FieldValueQuery is valid since DocValues are *required* for GeoPointField
+ return new FieldValueQuery(field);
+ }
+
if (maxLon < minLon) {
BooleanQuery.Builder bqb = new BooleanQuery.Builder();
diff --git a/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java b/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java
index 9187964ab2f..a9e487b8815 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java
@@ -22,6 +22,7 @@ import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.GeoPointField;
+import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.store.Directory;
@@ -103,7 +104,7 @@ public class TestGeoPointQuery extends BaseGeoPointTestCase {
new GeoPointField(FIELD_NAME, -83.99724648980559, 58.29438379542874, storedPoint),
new GeoPointField(FIELD_NAME, -26.779373834241003, 33.541429799076354, storedPoint),
new GeoPointField(FIELD_NAME, -77.35379276106497, 26.774024500421728, storedPoint),
- new GeoPointField(FIELD_NAME, -14.796283808944777, -62.455081198245665, storedPoint),
+ new GeoPointField(FIELD_NAME, -14.796283808944777, -90.0, storedPoint),
new GeoPointField(FIELD_NAME, -178.8538113027811, 32.94823588839368, storedPoint),
new GeoPointField(FIELD_NAME, 178.8538113027811, 32.94823588839368, storedPoint),
new GeoPointField(FIELD_NAME, -73.998776, 40.720611, storedPoint),
@@ -123,6 +124,13 @@ public class TestGeoPointQuery extends BaseGeoPointTestCase {
writer.addDocument(doc);
}
+ // index random string documents
+ for (int i=0; i<random().nextInt(10); ++i) {
+ Document doc = new Document();
+ doc.add(new StringField("string", Integer.toString(i), Field.Store.NO));
+ writer.addDocument(doc);
+ }
+
reader = writer.getReader();
searcher = newSearcher(reader);
writer.close();
@@ -264,7 +272,7 @@ public class TestGeoPointQuery extends BaseGeoPointTestCase {
}
public void testWholeMap() throws Exception {
- TopDocs td = bboxQuery(-179.9, -89.9, 179.9, 89.9, 20);
+ TopDocs td = bboxQuery(GeoUtils.MIN_LON_INCL, GeoUtils.MIN_LAT_INCL, GeoUtils.MAX_LON_INCL, GeoUtils.MAX_LAT_INCL, 20);
assertEquals("testWholeMap failed", 24, td.totalHits);
}