Skip to content

Apache Lucene open-source search software

LUCENE-7254: Switch geo queries back to DocIdSetBuilder.

Adrien Granddfa3d6c9d909
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointBoxQuery.java b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointBoxQuery.java
index 014e2a526d0..6898d0db4ef 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointBoxQuery.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointBoxQuery.java
@@ -34,6 +34,7 @@ import org.apache.lucene.search.Weight;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.util.DocIdSetBuilder;
import org.apache.lucene.util.StringHelper;
/**
@@ -103,19 +104,21 @@ abstract class LatLonPointBoxQuery extends Query {
return new ConstantScoreWeight(this) {
private DocIdSetIterator buildMatchingIterator(LeafReader reader, PointValues values) throws IOException {
- MatchingPoints result = new MatchingPoints(reader, field);
+ DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values, field);
values.intersect(field,
new IntersectVisitor() {
+ DocIdSetBuilder.BulkAdder adder;
+
@Override
public void grow(int count) {
- result.grow(count);
+ adder = result.grow(count);
}
@Override
public void visit(int docID) {
- result.add(docID);
+ adder.add(docID);
}
@Override
@@ -133,7 +136,7 @@ abstract class LatLonPointBoxQuery extends Query {
}
// Doc is in-bounds
- result.add(docID);
+ adder.add(docID);
}
@Override
@@ -160,7 +163,7 @@ abstract class LatLonPointBoxQuery extends Query {
}
}
});
- return result.iterator();
+ return result.build().iterator();
}
@Override
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java
index e3fd1125ccf..29fac798723 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java
@@ -32,6 +32,7 @@ import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Weight;
+import org.apache.lucene.util.DocIdSetBuilder;
import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.util.SloppyMath;
import org.apache.lucene.util.StringHelper;
@@ -117,18 +118,21 @@ final class LatLonPointDistanceQuery extends Query {
LatLonPoint.checkCompatible(fieldInfo);
// matching docids
- MatchingPoints result = new MatchingPoints(reader, field);
+ DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values, field);
values.intersect(field,
new IntersectVisitor() {
+
+ DocIdSetBuilder.BulkAdder adder;
+
@Override
public void grow(int count) {
- result.grow(count);
+ adder = result.grow(count);
}
@Override
public void visit(int docID) {
- result.add(docID);
+ adder.add(docID);
}
@Override
@@ -152,7 +156,7 @@ final class LatLonPointDistanceQuery extends Query {
// its a match only if its sortKey <= our sortKey
if (SloppyMath.haversinSortKey(latitude, longitude, docLatitude, docLongitude) <= sortKey) {
- result.add(docID);
+ adder.add(docID);
}
}
@@ -206,7 +210,7 @@ final class LatLonPointDistanceQuery extends Query {
}
});
- return new ConstantScoreScorer(this, score(), result.iterator());
+ return new ConstantScoreScorer(this, score(), result.build().iterator());
}
};
}
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java
index e90053755d0..8db829625cf 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java
@@ -32,6 +32,7 @@ import org.apache.lucene.index.PointValues;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.util.DocIdSetBuilder;
import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.util.StringHelper;
import org.apache.lucene.geo.Polygon;
@@ -110,25 +111,28 @@ final class LatLonPointInPolygonQuery extends Query {
LatLonPoint.checkCompatible(fieldInfo);
// matching docids
- MatchingPoints result = new MatchingPoints(reader, field);
+ DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values, field);
values.intersect(field,
new IntersectVisitor() {
+
+ DocIdSetBuilder.BulkAdder adder;
+
@Override
public void grow(int count) {
- result.grow(count);
+ adder = result.grow(count);
}
@Override
public void visit(int docID) {
- result.add(docID);
+ adder.add(docID);
}
@Override
public void visit(int docID, byte[] packedValue) {
if (tree.contains(decodeLatitude(packedValue, 0),
decodeLongitude(packedValue, Integer.BYTES))) {
- result.add(docID);
+ adder.add(docID);
}
}
@@ -151,7 +155,7 @@ final class LatLonPointInPolygonQuery extends Query {
}
});
- return new ConstantScoreScorer(this, score(), result.iterator());
+ return new ConstantScoreScorer(this, score(), result.build().iterator());
}
};
}
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/MatchingPoints.java b/lucene/sandbox/src/java/org/apache/lucene/document/MatchingPoints.java
deleted file mode 100644
index 4ef8ca96735..00000000000
--- a/lucene/sandbox/src/java/org/apache/lucene/document/MatchingPoints.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * 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.
- */
-package org.apache.lucene.document;
-
-import org.apache.lucene.index.LeafReader;
-import org.apache.lucene.index.PointValues;
-import org.apache.lucene.search.DocIdSetIterator;
-import org.apache.lucene.util.BitSet;
-import org.apache.lucene.util.BitSetIterator;
-import org.apache.lucene.util.FixedBitSet;
-import org.apache.lucene.util.SparseFixedBitSet;
-
-/**
- * Accumulates matching hits for points.
- * <p>
- * Add matches with ({@link #add(int)}) and call {@link #iterator()} for
- * an iterator over the results.
- * <p>
- * <b>NOTE:</b> it is required that you implement the optional {@code grow()}
- * method in your IntersectVisitor, this is used for cost computation.
- * <p>
- * This implementation currently optimizes bitset structure (sparse vs dense)
- * and {@link DocIdSetIterator#cost()} (cardinality) based on index statistics.
- * This API may change as point values evolves.
- *
- * @lucene.experimental
- */
-final class MatchingPoints {
- /** bitset we collect into */
- private final BitSet bits;
- /** number of documents containing a value for the points field */
- private final int docCount;
- /** number of values indexed for the points field */
- private final long numPoints;
- /** number of documents in the index segment */
- private final int maxDoc;
- /** counter of hits seen */
- private long counter;
-
- /**
- * Creates a new accumulator.
- * @param reader reader to collect point matches from
- * @param field field name.
- */
- public MatchingPoints(LeafReader reader, String field) {
- maxDoc = reader.maxDoc();
- PointValues values = reader.getPointValues();
- if (values == null) {
- throw new IllegalStateException("the query is missing null checks");
- }
- docCount = values.getDocCount(field);
- numPoints = values.size(field);
- // heuristic: if the field is really sparse, use a sparse impl
- if (docCount >= 0 && docCount * 100L < maxDoc) {
- bits = new SparseFixedBitSet(maxDoc);
- } else {
- bits = new FixedBitSet(maxDoc);
- }
- }
-
- /**
- * Record a matching docid.
- * <p>
- * NOTE: doc IDs do not need to be provided in any order.
- */
- public void add(int doc) {
- bits.set(doc);
- }
-
- /**
- * Grows cardinality counter by the given amount.
- */
- public void grow(int amount) {
- counter += amount;
- }
-
- /**
- * Returns an iterator over the recorded matches.
- */
- public DocIdSetIterator iterator() {
- // ensure caller implements the grow() api
- assert counter > 0 || bits.cardinality() == 0 : "the IntersectVisitor is missing grow()";
-
- // if single-valued (docCount == numPoints), then we know 1 point == 1 doc
- // otherwise we approximate based on field stats
- return new BitSetIterator(bits, (long) (counter * (docCount / (double) numPoints)));
- }
-}