Apache Lucene open-source search software
LUCENE6712: use doc-values for GeoPointField boundary cell post filtering
Michael McCandlessf211284f0baa
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 1e0c6b10cd7..790b49ca15b 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -162,6 +162,10 @@ New Features* LUCENE-6580: SpanNearQuery now allows defined-width gaps in its subqueries(Alan Woodward, Adrien Grand).+* LUCENE-6712: Use doc values to post-filter GeoPointField hits that+ fall in boundary cells, resulting in smaller index, faster searches+ and less heap used for each query (Nick Knize via Mike McCandless).+API Changes* LUCENE-6508: Simplify Lock api, there is now justdiff --git a/lucene/sandbox/src/java/org/apache/lucene/document/GeoPointField.java b/lucene/sandbox/src/java/org/apache/lucene/document/GeoPointField.javaindex e9ecc760ac3..03ca15a3dfc 100644--- a/lucene/sandbox/src/java/org/apache/lucene/document/GeoPointField.java+++ b/lucene/sandbox/src/java/org/apache/lucene/document/GeoPointField.java@@ -17,6 +17,7 @@ package org.apache.lucene.document;* limitations under the License.*/+import org.apache.lucene.index.DocValuesType;import org.apache.lucene.index.IndexOptions;import org.apache.lucene.util.GeoUtils;@@ -41,7 +42,7 @@ import org.apache.lucene.util.GeoUtils;* @lucene.experimental*/public final class GeoPointField extends Field {- public static final int PRECISION_STEP = 6;+ public static final int PRECISION_STEP = 9;/*** Type for an GeoPointField that is not stored:@@ -52,6 +53,7 @@ public final class GeoPointField extends Field {TYPE_NOT_STORED.setTokenized(false);TYPE_NOT_STORED.setOmitNorms(true);TYPE_NOT_STORED.setIndexOptions(IndexOptions.DOCS);+ TYPE_NOT_STORED.setDocValuesType(DocValuesType.SORTED_NUMERIC);TYPE_NOT_STORED.setNumericType(FieldType.NumericType.LONG);TYPE_NOT_STORED.setNumericPrecisionStep(PRECISION_STEP);TYPE_NOT_STORED.freeze();@@ -66,6 +68,7 @@ public final class GeoPointField extends Field {TYPE_STORED.setTokenized(false);TYPE_STORED.setOmitNorms(true);TYPE_STORED.setIndexOptions(IndexOptions.DOCS);+ TYPE_STORED.setDocValuesType(DocValuesType.SORTED_NUMERIC);TYPE_STORED.setNumericType(FieldType.NumericType.LONG);TYPE_STORED.setNumericPrecisionStep(PRECISION_STEP);TYPE_STORED.setStored(true);diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointDistanceQuery.java b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointDistanceQuery.javaindex c10746d0799..4ff86d3f394 100644--- a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointDistanceQuery.java+++ b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointDistanceQuery.java@@ -27,18 +27,16 @@ import org.apache.lucene.util.ToStringUtils;* like {@code GeoPointInBBoxQueryImpl} candidate terms are queried using the numeric ranges based on* the morton codes of the min and max lat/lon pairs that intersect the boundary of the point-radius* circle (see {@link org.apache.lucene.util.GeoUtils#lineCrossesSphere}. Terms- * passing this initial filter are then passed to a secondary filter that verifies whether the+ * passing this initial filter are then passed to a secondary {@code postFilter} method that verifies whether the* decoded lat/lon point fall within the specified query distance (see {@link org.apache.lucene.util.SloppyMath#haversin}.* All morton value comparisons are subject to the same precision tolerance defined in* {@value org.apache.lucene.util.GeoUtils#TOLERANCE} and distance comparisons are subject to the accuracy of the* haversine formula (from R.W. Sinnott, "Virtues of the Haversine", Sky and Telescope, vol. 68, no. 2, 1984, p. 159)*- * NOTE: This query works best for point-radius queries that do not cross the dateline, there is a penalty for crossing- * the dateline as the bounding box is effectively split into two separate queries, and the point-radius is converted- * to a euclidean spherical search to handle a wrapping coordinate system (TODO split the point radius at the dateline?)*- * This query also currently uses haversine which is a sloppy distance calculation. For large queries one can expect- * upwards of 400m error. Vincenty shrinks this to ~40m error but pays a penalty for computing using the spheroid+ * Note: This query currently uses haversine which is a sloppy distance calculation (see above reference). For large+ * queries one can expect upwards of 400m error. Vincenty shrinks this to ~40m error but pays a penalty for computing+ * using the spheroid** @lucene.experimental*/diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointDistanceQueryImpl.java b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointDistanceQueryImpl.javaindex 180504ed8f1..988b00b5507 100644--- a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointDistanceQueryImpl.java+++ b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointDistanceQueryImpl.java@@ -22,9 +22,7 @@ import java.io.IOException;import org.apache.lucene.index.Terms;import org.apache.lucene.index.TermsEnum;import org.apache.lucene.util.AttributeSource;-import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.GeoUtils;-import org.apache.lucene.util.NumericUtils;import org.apache.lucene.util.SloppyMath;/** Package private implementation for the public facing GeoPointDistanceQuery delegate class.@@ -67,30 +65,19 @@ final class GeoPointDistanceQueryImpl extends GeoPointInBBoxQueryImpl {@Overrideprotected boolean cellIntersectsShape(final double minLon, final double minLat, final double maxLon, final double maxLat) {- return (cellCrosses(minLon, minLat, maxLon, maxLat) || cellContains(minLon, minLat, maxLon, maxLat)- || cellWithin(minLon, minLat, maxLon, maxLat));+ return (cellContains(minLon, minLat, maxLon, maxLat)+ || cellWithin(minLon, minLat, maxLon, maxLat) || cellCrosses(minLon, minLat, maxLon, maxLat));}/**- * The two-phase query approach. The parent- * {@link org.apache.lucene.search.GeoPointTermsEnum#accept} method is called to match- * encoded terms that fall within the bounding box of the polygon. Those documents that pass the initial- * bounding box filter are then compared to the provided distance using the+ * The two-phase query approach. The parent {@link org.apache.lucene.search.GeoPointTermsEnum} class matches+ * encoded terms that fall within the minimum bounding box of the point-radius circle. Those documents that pass+ * the initial bounding box filter are then post filter compared to the provided distance using the* {@link org.apache.lucene.util.SloppyMath#haversin} method.- *- * @param term term for candidate document- * @return match status*/@Override- protected AcceptStatus postFilterBoundary(BytesRef term) {- final long val = NumericUtils.prefixCodedToLong(term);- final double lon = GeoUtils.mortonUnhashLon(val);- final double lat = GeoUtils.mortonUnhashLat(val);- // post-filter by distance- if (SloppyMath.haversin(query.centerLat, query.centerLon, lat, lon) * 1000.0 > query.radius) {- return AcceptStatus.NO;- }- return AcceptStatus.YES;+ protected boolean postFilter(final double lon, final double lat) {+ return (SloppyMath.haversin(query.centerLat, query.centerLon, lat, lon) * 1000.0 <= query.radius);}}diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInBBoxQueryImpl.java b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInBBoxQueryImpl.javaindex 9f248548117..b052a81eb7d 100644--- a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInBBoxQueryImpl.java+++ b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInBBoxQueryImpl.java@@ -22,6 +22,7 @@ import java.io.IOException;import org.apache.lucene.index.Terms;import org.apache.lucene.index.TermsEnum;import org.apache.lucene.util.AttributeSource;+import org.apache.lucene.util.GeoUtils;import org.apache.lucene.util.ToStringUtils;/** Package private implementation for the public facing GeoPointInBBoxQuery delegate class.@@ -44,7 +45,7 @@ class GeoPointInBBoxQueryImpl extends GeoPointTermQuery {@Override @SuppressWarnings("unchecked")protected TermsEnum getTermsEnum(final Terms terms, AttributeSource atts) throws IOException {- return new GeoPointTermsEnum(terms.iterator(), minLon, minLat, maxLon, maxLat);+ return new GeoPointInBBoxTermsEnum(terms.iterator(), minLon, minLat, maxLon, maxLat);}@Override@@ -52,6 +53,39 @@ class GeoPointInBBoxQueryImpl extends GeoPointTermQuery {throw new UnsupportedOperationException("cannot change rewrite method");}+ protected class GeoPointInBBoxTermsEnum extends GeoPointTermsEnum {+ protected GeoPointInBBoxTermsEnum(final TermsEnum tenum, final double minLon, final double minLat,+ final double maxLon, final double maxLat) {+ super(tenum, minLon, minLat, maxLon, maxLat);+ }++ /**+ * Determine whether the quad-cell crosses the shape+ */+ @Override+ protected boolean cellCrosses(final double minLon, final double minLat, final double maxLon, final double maxLat) {+ return GeoUtils.rectCrosses(minLon, minLat, maxLon, maxLat, this.minLon, this.minLat, this.maxLon, this.maxLat);+ }++ /**+ * Determine whether quad-cell is within the shape+ */+ @Override+ protected boolean cellWithin(final double minLon, final double minLat, final double maxLon, final double maxLat) {+ return GeoUtils.rectWithin(minLon, minLat, maxLon, maxLat, this.minLon, this.minLat, this.maxLon, this.maxLat);+ }++ @Override+ protected boolean cellIntersectsShape(final double minLon, final double minLat, final double maxLon, final double maxLat) {+ return cellIntersectsMBR(minLon, minLat, maxLon, maxLat);+ }++ @Override+ protected boolean postFilter(final double lon, final double lat) {+ return GeoUtils.bboxContains(lon, lat, minLon, minLat, maxLon, maxLat);+ }+ }+@Override@SuppressWarnings({"unchecked","rawtypes"})public boolean equals(Object o) {diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInPolygonQuery.java b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInPolygonQuery.javaindex b4ad8b64ff4..cb446672a4b 100644--- a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInPolygonQuery.java+++ b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointInPolygonQuery.java@@ -20,9 +20,7 @@ package org.apache.lucene.search;import org.apache.lucene.index.Terms;import org.apache.lucene.index.TermsEnum;import org.apache.lucene.util.AttributeSource;-import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.GeoUtils;-import org.apache.lucene.util.NumericUtils;import org.apache.lucene.util.ToStringUtils;import java.io.IOException;@@ -169,9 +167,8 @@ public final class GeoPointInPolygonQuery extends GeoPointInBBoxQueryImpl {}@Override- protected boolean cellIntersectsMBR(final double minLon, final double minLat, final double maxLon, final double maxLat) {- return GeoUtils.rectIntersects(minLon, minLat, maxLon, maxLat, GeoPointInPolygonQuery.this.minLon,- GeoPointInPolygonQuery.this.minLat, GeoPointInPolygonQuery.this.maxLon, GeoPointInPolygonQuery.this.maxLat);+ protected boolean cellIntersectsShape(final double minLon, final double minLat, final double maxLon, final double maxLat) {+ return cellWithin(minLon, minLat, maxLon, maxLat) || cellCrosses(minLon, minLat, maxLon, maxLat);}/**@@ -180,20 +177,10 @@ public final class GeoPointInPolygonQuery extends GeoPointInBBoxQueryImpl {* encoded terms that fall within the bounding box of the polygon. Those documents that pass the initial* bounding box filter are then compared to the provided polygon using the* {@link org.apache.lucene.util.GeoUtils#pointInPolygon} method.- *- * @param term term for candidate document- * @return match status*/@Override- protected AcceptStatus postFilterBoundary(BytesRef term) {- final long val = NumericUtils.prefixCodedToLong(term);- final double lon = GeoUtils.mortonUnhashLon(val);- final double lat = GeoUtils.mortonUnhashLat(val);- // post-filter by point in polygon- if (!GeoUtils.pointInPolygon(x, y, lat, lon)) {- return AcceptStatus.NO;- }- return AcceptStatus.YES;+ protected boolean postFilter(final double lon, final double lat) {+ return GeoUtils.pointInPolygon(x, y, lat, lon);}}diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermQuery.java b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermQuery.javaindex 5f8f39404d3..7f7fd13746a 100644--- a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermQuery.java+++ b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermQuery.java@@ -17,6 +17,7 @@ package org.apache.lucene.search;* limitations under the License.*/+import org.apache.lucene.index.IndexReader;import org.apache.lucene.util.GeoUtils;/**@@ -25,8 +26,6 @@ import org.apache.lucene.util.GeoUtils;** @lucene.experimental*/--// TODO: remove this? Just absorb into its base classabstract class GeoPointTermQuery extends MultiTermQuery {// simple bounding box optimization - no objects used to avoid dependenciesprotected final double minLon;@@ -57,5 +56,18 @@ abstract class GeoPointTermQuery extends MultiTermQuery {this.minLat = minLat;this.maxLon = maxLon;this.maxLat = maxLat;++ this.rewriteMethod = GEO_CONSTANT_SCORE_REWRITE;}++ public static final RewriteMethod GEO_CONSTANT_SCORE_REWRITE = new RewriteMethod() {+ @Override+ public Query rewrite(IndexReader reader, MultiTermQuery query) {+ Query result = new GeoPointTermQueryConstantScoreWrapper<>((GeoPointTermQuery)query);+ result.setBoost(query.getBoost());+ return result;+ }+ };++}diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermQueryConstantScoreWrapper.java b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermQueryConstantScoreWrapper.javanew file mode 100644index 00000000000..a3cd4821c19--- /dev/null+++ b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermQueryConstantScoreWrapper.java@@ -0,0 +1,130 @@+package org.apache.lucene.search;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import java.io.IOException;++import org.apache.lucene.index.LeafReader;+import org.apache.lucene.index.LeafReaderContext;+import org.apache.lucene.index.PostingsEnum;+import org.apache.lucene.index.SortedNumericDocValues;+import org.apache.lucene.index.Terms;+import org.apache.lucene.util.DocIdSetBuilder;+import org.apache.lucene.util.GeoUtils;++/**+ * Custom ConstantScoreWrapper for {@code GeoPointTermQuery} that cuts over to DocValues+ * for post filtering boundary ranges. Multi-valued GeoPoint documents are supported.+ *+ * @lucene.experimental+ */+final class GeoPointTermQueryConstantScoreWrapper <Q extends GeoPointTermQuery> extends Query {+ protected final Q query;++ protected GeoPointTermQueryConstantScoreWrapper(Q query) {+ this.query = query;+ }++ @Override+ public String toString(String field) {+ return query.toString();+ }++ @Override+ public final boolean equals(final Object o) {+ if (super.equals(o) == false) {+ return false;+ }+ final GeoPointTermQueryConstantScoreWrapper<?> that = (GeoPointTermQueryConstantScoreWrapper<?>) o;+ return this.query.equals(that.query) && this.getBoost() == that.getBoost();+ }++ @Override+ public final int hashCode() {+ return 31 * super.hashCode() + query.hashCode();+ }++ @Override+ public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {+ return new ConstantScoreWeight(this) {++ private DocIdSet getDocIDs(LeafReaderContext context) throws IOException {+ final Terms terms = context.reader().terms(query.field);+ if (terms == null) {+ return DocIdSet.EMPTY;+ }++ final GeoPointTermsEnum termsEnum = (GeoPointTermsEnum)(query.getTermsEnum(terms));+ assert termsEnum != null;++ LeafReader reader = context.reader();+ DocIdSetBuilder builder = new DocIdSetBuilder(reader.maxDoc());+ PostingsEnum docs = null;+ SortedNumericDocValues sdv = reader.getSortedNumericDocValues(query.field);++ while (termsEnum.next() != null) {+ docs = termsEnum.postings(docs, PostingsEnum.NONE);+ // boundary terms need post filtering by+ if (termsEnum.boundaryTerm()) {+ int docId = docs.nextDoc();+ do {+ sdv.setDocument(docId);+ for (int i=0; i<sdv.count(); ++i) {+ final long hash = sdv.valueAt(i);+ final double lon = GeoUtils.mortonUnhashLon(hash);+ final double lat = GeoUtils.mortonUnhashLat(hash);+ if (termsEnum.postFilter(lon, lat)) {+ builder.add(docId);+ }+ }+ } while ((docId = docs.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS);+ } else {+ builder.add(docs);+ }+ }++ return builder.build();+ }++ private Scorer scorer(DocIdSet set) throws IOException {+ if (set == null) {+ return null;+ }+ final DocIdSetIterator disi = set.iterator();+ if (disi == null) {+ return null;+ }+ return new ConstantScoreScorer(this, score(), disi);+ }++ @Override+ public BulkScorer bulkScorer(LeafReaderContext context) throws IOException {+ final Scorer scorer = scorer(getDocIDs(context));+ if (scorer == null) {+ return null;+ }+ return new DefaultBulkScorer(scorer);+ }++ @Override+ public Scorer scorer(LeafReaderContext context) throws IOException {+ return scorer(getDocIDs(context));+ }+ };+ }+}diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermsEnum.java b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermsEnum.javaindex 37d30ec78f9..15d9ee15ba1 100644--- a/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermsEnum.java+++ b/lucene/sandbox/src/java/org/apache/lucene/search/GeoPointTermsEnum.java@@ -17,7 +17,6 @@ package org.apache.lucene.search;* limitations under the License.*/-import java.math.BigInteger;import java.util.Collections;import java.util.LinkedList;import java.util.List;@@ -36,18 +35,21 @@ import org.apache.lucene.util.NumericUtils;** @lucene.experimental*/-class GeoPointTermsEnum extends FilteredTermsEnum {+abstract class GeoPointTermsEnum extends FilteredTermsEnum {protected final double minLon;protected final double minLat;protected final double maxLon;protected final double maxLat;protected Range currentRange;- private BytesRef currentLowerBound, currentUpperBound;+ private BytesRef currentCell;private final List<Range> rangeBounds = new LinkedList<>();- protected static final short DETAIL_LEVEL = 16;+ // detail level should be a factor of PRECISION_STEP limiting the depth of recursion (and number of ranges)+ // in this case a factor of 4 brings the detail level to ~0.002/0.001 degrees lon/lat respectively (or ~222m/111m)+ private static final short MAX_SHIFT = GeoPointField.PRECISION_STEP * 4;+ protected static final short DETAIL_LEVEL = ((GeoUtils.BITS<<1)-MAX_SHIFT)/2;GeoPointTermsEnum(final TermsEnum tenum, final double minLon, final double minLat,final double maxLon, final double maxLat) {@@ -100,7 +102,7 @@ class GeoPointTermsEnum extends FilteredTermsEnum {// if cell is within and a factor of the precision step, or it crosses the edge of the shape add the rangefinal boolean within = res % GeoPointField.PRECISION_STEP == 0 && cellWithin(minLon, minLat, maxLon, maxLat);if (within || (level == DETAIL_LEVEL && cellIntersectsShape(minLon, minLat, maxLon, maxLat))) {- rangeBounds.add(new Range(start, end, res, level, !within));+ rangeBounds.add(new Range(start, res, level, !within));} else if (level < DETAIL_LEVEL && cellIntersectsMBR(minLon, minLat, maxLon, maxLat)) {computeRange(start, (short) (res - 1));}@@ -109,30 +111,17 @@ class GeoPointTermsEnum extends FilteredTermsEnum {/*** Determine whether the quad-cell crosses the shape*/- protected boolean cellCrosses(final double minLon, final double minLat, final double maxLon, final double maxLat) {- return GeoUtils.rectCrosses(minLon, minLat, maxLon, maxLat, this.minLon, this.minLat, this.maxLon, this.maxLat);- }+ protected abstract boolean cellCrosses(final double minLon, final double minLat, final double maxLon, final double maxLat);/*** Determine whether quad-cell is within the shape*/- protected boolean cellWithin(final double minLon, final double minLat, final double maxLon, final double maxLat) {- return GeoUtils.rectWithin(minLon, minLat, maxLon, maxLat, this.minLon, this.minLat, this.maxLon, this.maxLat);- }-- /**- * Return whether quad-cell contains the bounding box of this shape- */- protected boolean cellContains(final double minLon, final double minLat, final double maxLon, final double maxLat) {- return GeoUtils.rectWithin(this.minLon, this.minLat, this.maxLon, this.maxLat, minLon, minLat, maxLon, maxLat);- }+ protected abstract boolean cellWithin(final double minLon, final double minLat, final double maxLon, final double maxLat);/*** Default shape is a rectangle, so this returns the same as {@code cellIntersectsMBR}*/- protected boolean cellIntersectsShape(final double minLon, final double minLat, final double maxLon, final double maxLat) {- return cellIntersectsMBR(minLon, minLat, maxLon, maxLat);- }+ protected abstract boolean cellIntersectsShape(final double minLon, final double minLat, final double maxLon, final double maxLat);/*** Primary driver for cells intersecting shape boundaries@@ -141,13 +130,23 @@ class GeoPointTermsEnum extends FilteredTermsEnum {return GeoUtils.rectIntersects(minLon, minLat, maxLon, maxLat, this.minLon, this.minLat, this.maxLon, this.maxLat);}+ /**+ * Return whether quad-cell contains the bounding box of this shape+ */+ protected boolean cellContains(final double minLon, final double minLat, final double maxLon, final double maxLat) {+ return GeoUtils.rectWithin(this.minLon, this.minLat, this.maxLon, this.maxLat, minLon, minLat, maxLon, maxLat);+ }++ public boolean boundaryTerm() {+ if (currentRange == null) {+ throw new IllegalStateException("GeoPointTermsEnum empty or not initialized");+ }+ return currentRange.boundary;+ }+private void nextRange() {currentRange = rangeBounds.remove(0);- currentLowerBound = currentRange.lower;- assert currentUpperBound == null || currentUpperBound.compareTo(currentRange.lower) <= 0 :- "The current upper bound must be <= the new lower bound";-- currentUpperBound = currentRange.upper;+ currentCell = currentRange.cell;}@Override@@ -158,20 +157,20 @@ class GeoPointTermsEnum extends FilteredTermsEnum {}// if the new upper bound is before the term parameter, the sub-range is never a hit- if (term != null && term.compareTo(currentUpperBound) > 0) {+ if (term != null && term.compareTo(currentCell) > 0) {nextRange();if (!rangeBounds.isEmpty()) {continue;}}// never seek backwards, so use current term if lower bound is smaller- return (term != null && term.compareTo(currentLowerBound) > 0) ?- term : currentLowerBound;+ return (term != null && term.compareTo(currentCell) > 0) ?+ term : currentCell;}// no more sub-range enums availableassert rangeBounds.isEmpty();- currentLowerBound = currentUpperBound = null;+ currentCell = null;return null;}@@ -186,58 +185,43 @@ class GeoPointTermsEnum extends FilteredTermsEnum {@Overrideprotected AcceptStatus accept(BytesRef term) {// validate value is in range- while (currentUpperBound == null || term.compareTo(currentUpperBound) > 0) {+ while (currentCell == null || term.compareTo(currentCell) > 0) {if (rangeBounds.isEmpty()) {return AcceptStatus.END;}// peek next sub-range, only seek if the current term is smaller than next lower bound- if (term.compareTo(rangeBounds.get(0).lower) < 0) {+ if (term.compareTo(rangeBounds.get(0).cell) < 0) {return AcceptStatus.NO_AND_SEEK;}- // step forward to next range without seeking, as next lower range bound is less or equal current term+ // step forward to next range without seeking, as next range is less or equal current termnextRange();}- if (currentRange.boundary) {- return postFilterBoundary(term);- }-return AcceptStatus.YES;}- protected AcceptStatus postFilterBoundary(BytesRef term) {- final long val = NumericUtils.prefixCodedToLong(term);- final double lon = GeoUtils.mortonUnhashLon(val);- final double lat = GeoUtils.mortonUnhashLat(val);- if (!GeoUtils.bboxContains(lon, lat, minLon, minLat, maxLon, maxLat)) {- return AcceptStatus.NO;- }- return AcceptStatus.YES;- }+ protected abstract boolean postFilter(final double lon, final double lat);/*** Internal class to represent a range along the space filling curve*/protected final class Range implements Comparable<Range> {- final BytesRef lower;- final BytesRef upper;+ final BytesRef cell;final short level;final boolean boundary;- Range(final long lower, final long upper, final short res, final short level, boolean boundary) {+ Range(final long lower, final short res, final short level, boolean boundary) {this.level = level;this.boundary = boundary;BytesRefBuilder brb = new BytesRefBuilder();- NumericUtils.longToPrefixCodedBytes(lower, boundary ? 0 : res, brb);- this.lower = brb.get();- NumericUtils.longToPrefixCodedBytes(upper, boundary ? 0 : res, (brb = new BytesRefBuilder()));- this.upper = brb.get();+ NumericUtils.longToPrefixCodedBytes(lower, res, brb);+ this.cell = brb.get();}@Overridepublic int compareTo(Range other) {- return this.lower.compareTo(other.lower);+ return this.cell.compareTo(other.cell);}}}diff --git a/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java b/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.javaindex 5b1e9dd1fd0..dc957ae4e83 100644--- a/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java+++ b/lucene/sandbox/src/test/org/apache/lucene/search/TestGeoPointQuery.java@@ -82,11 +82,12 @@ public class TestGeoPointQuery extends LuceneTestCase {final FieldType storedPoint = new FieldType(GeoPointField.TYPE_STORED);// this is a simple systematic testGeoPointField[] pts = new GeoPointField[] {- new GeoPointField(FIELD_NAME, -96.4538113027811, 32.94823588839368, storedPoint),+ new GeoPointField(FIELD_NAME, -96.774, 32.763420, storedPoint),new GeoPointField(FIELD_NAME, -96.7759895324707, 32.7559529921407, storedPoint),new GeoPointField(FIELD_NAME, -96.77701950073242, 32.77866942010977, storedPoint),new GeoPointField(FIELD_NAME, -96.7706036567688, 32.7756745755423, storedPoint),new GeoPointField(FIELD_NAME, -139.73458170890808, 27.703618681345585, storedPoint),+ new GeoPointField(FIELD_NAME, -96.4538113027811, 32.94823588839368, storedPoint),new GeoPointField(FIELD_NAME, -96.65084838867188, 33.06047141970814, storedPoint),new GeoPointField(FIELD_NAME, -96.7772, 32.778650, storedPoint),new GeoPointField(FIELD_NAME, -83.99724648980559, 58.29438379542874, storedPoint),@@ -103,6 +104,15 @@ public class TestGeoPointQuery extends LuceneTestCase {doc.add(p);writer.addDocument(doc);}++ // add explicit multi-valued docs+ for (int i=0; i<pts.length; i+=2) {+ Document doc = new Document();+ doc.add(pts[i]);+ doc.add(pts[i+1]);+ writer.addDocument(doc);+ }+reader = writer.getReader();searcher = newSearcher(reader);writer.close();@@ -135,7 +145,7 @@ public class TestGeoPointQuery extends LuceneTestCase {@Testpublic void testBBoxQuery() throws Exception {TopDocs td = bboxQuery(-96.7772, 32.778650, -96.77690000, 32.778950, 5);- assertEquals("GeoBoundingBoxQuery failed", 2, td.totalHits);+ assertEquals("GeoBoundingBoxQuery failed", 4, td.totalHits);}@Test@@ -144,7 +154,7 @@ public class TestGeoPointQuery extends LuceneTestCase {-96.6041564, -96.7449188, -96.76826477, -96.7682647},new double[]{33.073130, 32.9942669, 32.938386, 33.0374494,33.1369762, 33.1162747, 33.073130, 33.073130}, 5);- assertEquals("GeoPolygonQuery failed", 1, td.totalHits);+ assertEquals("GeoPolygonQuery failed", 2, td.totalHits);}@Test@@ -174,19 +184,19 @@ public class TestGeoPointQuery extends LuceneTestCase {@Testpublic void testBBoxCrossDateline() throws Exception {TopDocs td = bboxQuery(179.0, -45.0, -179.0, -44.0, 20);- assertEquals("BBoxCrossDateline query failed", 1, td.totalHits);+ assertEquals("BBoxCrossDateline query failed", 2, td.totalHits);}@Testpublic void testWholeMap() throws Exception {TopDocs td = bboxQuery(-179.9, -89.9, 179.9, 89.9, 20);- assertEquals("testWholeMap failed", 15, td.totalHits);+ assertEquals("testWholeMap failed", 24, td.totalHits);}@Testpublic void smallTest() throws Exception {TopDocs td = geoDistanceQuery(-73.998776, 40.720611, 1, 20);- assertEquals("smallTest failed", 1, td.totalHits);+ assertEquals("smallTest failed", 2, td.totalHits);}@Test@@ -202,22 +212,29 @@ public class TestGeoPointQuery extends LuceneTestCase {@Testpublic void testGeoDistanceQuery() throws Exception {TopDocs td = geoDistanceQuery(-96.4538113027811, 32.94823588839368, 6000, 20);- assertEquals("GeoDistanceQuery failed", 1, td.totalHits);+ assertEquals("GeoDistanceQuery failed", 2, td.totalHits);+ }++ @Test+ public void testMultiValuedQuery() throws Exception {+ TopDocs td = bboxQuery(-96.4538113027811, 32.7559529921407, -96.7706036567688, 32.7756745755423, 20);+ // 3 single valued docs + 2 multi-valued docs+ assertEquals("testMultiValuedQuery failed", 5, td.totalHits);}/**- * LUCENE-6704+ * Explicitly large*/@Nightlypublic void testGeoDistanceQueryHuge() throws Exception {- TopDocs td = geoDistanceQuery(-96.4538113027811, 32.94823588839368, 1000000, 20);- assertEquals("GeoDistanceQuery failed", 6, td.totalHits);+ TopDocs td = geoDistanceQuery(-96.4538113027811, 32.94823588839368, 2000000, 20);+ assertEquals("GeoDistanceQuery failed", 13, td.totalHits);}@Testpublic void testGeoDistanceQueryCrossDateline() throws Exception {TopDocs td = geoDistanceQuery(-179.9538113027811, 32.94823588839368, 120000, 20);- assertEquals("GeoDistanceQuery failed", 2, td.totalHits);+ assertEquals("GeoDistanceQuery failed", 3, td.totalHits);}@Test