Skip to content

Apache Lucene open-source search software

LUCENE-8914: Move compare logic to IntersectVisitor in FloatPointNearestNeighbor (#783)

Ignacio Veraf026053d4d82
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 86f6e0f932e..94726d83123 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -54,6 +54,9 @@ Improvements
* LUCENE-8894: Add APIs to find SPI names for Tokenizer/CharFilter/TokenFilter factory classes. (Tomoko Uchida)
+* LUCENE-8914: move the logic for discarding inner modes to the IntersectVisitor so we take advantage
+ of the change introduced in LUCENE-7862. (Ignacio Vera)
+
Other
* LUCENE-8778 LUCENE-8911: Define analyzer SPI names as static final fields and document the names in Javadocs.
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/FloatPointNearestNeighbor.java b/lucene/sandbox/src/java/org/apache/lucene/document/FloatPointNearestNeighbor.java
index 38a41038ca1..eb3db1aa593 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/document/FloatPointNearestNeighbor.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/document/FloatPointNearestNeighbor.java
@@ -190,6 +190,18 @@ public class FloatPointNearestNeighbor {
@Override
public PointValues.Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
+ for (int d = 0, offset = 0; d < dims; ++d, offset += Float.BYTES) {
+ float cellMaxAtDim = FloatPoint.decodeDimension(maxPackedValue, offset);
+ if (cellMaxAtDim < min[d]) {
+ // System.out.println(" skipped because cell max at " + d + " (" + cellMaxAtDim + ") < visitor.min[" + d + "] (" + min[d] + ")");
+ return PointValues.Relation.CELL_OUTSIDE_QUERY;
+ }
+ float cellMinAtDim = FloatPoint.decodeDimension(minPackedValue, offset);
+ if (cellMinAtDim > max[d]) {
+ // System.out.println(" skipped because cell min at " + d + " (" + cellMinAtDim + ") > visitor.max[" + d + "] (" + max[d] + ")");
+ return PointValues.Relation.CELL_OUTSIDE_QUERY;
+ }
+ }
return PointValues.Relation.CELL_CROSSES_QUERY;
}
}
@@ -243,7 +255,7 @@ public class FloatPointNearestNeighbor {
approxBestDistanceSquared(minPackedValue, maxPackedValue, origin)));
}
- LOOP_OVER_CELLS: while (cellQueue.size() > 0) {
+ while (cellQueue.size() > 0) {
Cell cell = cellQueue.poll();
// System.out.println(" visit " + cell);
@@ -262,20 +274,9 @@ public class FloatPointNearestNeighbor {
// Non-leaf block: split into two cells and put them back into the queue:
if (hitQueue.size() == topN) {
- for (int d = 0, offset = 0; d < visitor.dims; ++d, offset += Float.BYTES) {
- float cellMaxAtDim = FloatPoint.decodeDimension(cell.maxPacked, offset);
- float cellMinAtDim = FloatPoint.decodeDimension(cell.minPacked, offset);
- if (cellMaxAtDim < visitor.min[d] || cellMinAtDim > visitor.max[d]) {
- // this cell is outside our search radius; don't bother exploring any more
-
- // if (cellMaxAtDim < visitor.min[d]) {
- // System.out.println(" skipped because cell max at " + d + " (" + cellMaxAtDim + ") < visitor.min[" + d + "] (" + visitor.min[d] + ")");
- // } else {
- // System.out.println(" skipped because cell min at " + d + " (" + cellMinAtDim + ") > visitor.max[" + d + "] (" + visitor.max[d] + ")");
- // }
-
- continue LOOP_OVER_CELLS;
- }
+ if (visitor.compare(cell.minPacked, cell.maxPacked) == PointValues.Relation.CELL_OUTSIDE_QUERY) {
+ // this cell is outside our search radius; don't bother exploring any more
+ continue;
}
}
BytesRef splitValue = BytesRef.deepCopyOf(cell.index.getSplitDimValue());