You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by be...@apache.org on 2025/09/10 13:23:41 UTC
(lucene) branch branch_10x updated: integrate into new bulk scorers (#15021)
This is an automated email from the ASF dual-hosted git repository.
benwtrent pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/lucene.git
The following commit(s) were added to refs/heads/branch_10x by this push:
new 66d7ed8aaa2 integrate into new bulk scorers (#15021)
66d7ed8aaa2 is described below
commit 66d7ed8aaa231ba43794d0aee0fffbcf880355d3
Author: Trevor McCulloch <tr...@mongodb.com>
AuthorDate: Wed Sep 10 06:16:46 2025 -0700
integrate into new bulk scorers (#15021)
Return a little extra information from the bulkScore() as this will allow callers to skip results that are
below the minimum similarity needed to appear in the result set. Utilize this in hnsw and exhaustive search
to skip any topN heap manipulation for any bulk scored batches that do not meet the minimum score.
See #14013
---
.../codecs/lucene99/Lucene99HnswVectorsReader.java | 18 ++++++++++--------
.../apache/lucene/search/AbstractKnnCollector.java | 2 +-
.../apache/lucene/util/hnsw/HnswGraphSearcher.java | 9 +++++----
.../apache/lucene/util/hnsw/RandomVectorScorer.java | 6 +++++-
.../Lucene99MemorySegmentFloatVectorScorer.java | 19 ++++++++++++++++---
...ucene99MemorySegmentFloatVectorScorerSupplier.java | 19 ++++++++++++++++---
.../lucene/codecs/hnsw/TestFlatVectorScorer.java | 8 ++++++--
7 files changed, 59 insertions(+), 22 deletions(-)
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
index fed53ddff47..170f9dbd4dd 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java
@@ -367,10 +367,11 @@ public final class Lucene99HnswVectorsReader extends KnnVectorsReader
}
ords[numOrds++] = i;
if (numOrds == ords.length) {
- scorer.bulkScore(ords, scores, numOrds);
- for (int j = 0; j < numOrds; j++) {
- knnCollector.incVisitedCount(1);
- knnCollector.collect(scorer.ordToDoc(ords[j]), scores[j]);
+ knnCollector.incVisitedCount(numOrds);
+ if (scorer.bulkScore(ords, scores, numOrds) > knnCollector.minCompetitiveSimilarity()) {
+ for (int j = 0; j < numOrds; j++) {
+ knnCollector.collect(scorer.ordToDoc(ords[j]), scores[j]);
+ }
}
numOrds = 0;
}
@@ -378,10 +379,11 @@ public final class Lucene99HnswVectorsReader extends KnnVectorsReader
}
if (numOrds > 0) {
- scorer.bulkScore(ords, scores, numOrds);
- for (int j = 0; j < numOrds; j++) {
- knnCollector.incVisitedCount(1);
- knnCollector.collect(scorer.ordToDoc(ords[j]), scores[j]);
+ knnCollector.incVisitedCount(numOrds);
+ if (scorer.bulkScore(ords, scores, numOrds) > knnCollector.minCompetitiveSimilarity()) {
+ for (int j = 0; j < numOrds; j++) {
+ knnCollector.collect(scorer.ordToDoc(ords[j]), scores[j]);
+ }
}
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/search/AbstractKnnCollector.java b/lucene/core/src/java/org/apache/lucene/search/AbstractKnnCollector.java
index 34484559d0b..2fefcc9abbe 100644
--- a/lucene/core/src/java/org/apache/lucene/search/AbstractKnnCollector.java
+++ b/lucene/core/src/java/org/apache/lucene/search/AbstractKnnCollector.java
@@ -47,7 +47,7 @@ public abstract class AbstractKnnCollector implements KnnCollector {
@Override
public final void incVisitedCount(int count) {
- assert count > 0;
+ assert count >= 0;
this.visitedCount += count;
}
diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSearcher.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSearcher.java
index cd9b013d27a..4e942031d1d 100644
--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSearcher.java
+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSearcher.java
@@ -335,10 +335,11 @@ public class HnswGraphSearcher extends AbstractHnswGraphSearcher {
bulkNodes[numNodes++] = friendOrd;
}
- if (numNodes > 0) {
- numNodes = (int) Math.min((long) numNodes, results.visitLimit() - results.visitedCount());
- scorer.bulkScore(bulkNodes, bulkScores, numNodes);
- results.incVisitedCount(numNodes);
+ numNodes = (int) Math.min((long) numNodes, results.visitLimit() - results.visitedCount());
+ results.incVisitedCount(numNodes);
+ if (numNodes > 0
+ && scorer.bulkScore(bulkNodes, bulkScores, numNodes)
+ > results.minCompetitiveSimilarity()) {
for (int i = 0; i < numNodes; i++) {
int node = bulkNodes[i];
float score = bulkScores[i];
diff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/RandomVectorScorer.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/RandomVectorScorer.java
index d481256d7a5..f8fb4593eb7 100644
--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/RandomVectorScorer.java
+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/RandomVectorScorer.java
@@ -42,11 +42,15 @@ public interface RandomVectorScorer {
* @param nodes array of nodes to score.
* @param scores output array of scores corresponding to each node.
* @param numNodes number of nodes to score. Must not exceed length of nodes or scores arrays.
+ * @return the maximum scored value of any node, or Float.NEGATIVE_INFINITY if numNodes == 0.
*/
- default void bulkScore(int[] nodes, float[] scores, int numNodes) throws IOException {
+ default float bulkScore(int[] nodes, float[] scores, int numNodes) throws IOException {
+ float max = Float.NEGATIVE_INFINITY;
for (int i = 0; i < numNodes; i++) {
scores[i] = score(nodes[i]);
+ max = Math.max(max, scores[i]);
}
+ return max;
}
/**
diff --git a/lucene/core/src/java21/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorer.java b/lucene/core/src/java21/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorer.java
index 75a9b167ca0..b24a1a5f859 100644
--- a/lucene/core/src/java21/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorer.java
+++ b/lucene/core/src/java21/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorer.java
@@ -81,9 +81,10 @@ abstract sealed class Lucene99MemorySegmentFloatVectorScorer
}
@Override
- public void bulkScore(int[] nodes, float[] scores, int numNodes) throws IOException {
+ public float bulkScore(int[] nodes, float[] scores, int numNodes) throws IOException {
int i = 0;
final int limit = numNodes & ~3;
+ float maxScore = Float.NEGATIVE_INFINITY;
for (; i < limit; i += 4) {
long offset1 = (long) nodes[i] * vectorByteSize;
long offset2 = (long) nodes[i + 1] * vectorByteSize;
@@ -91,9 +92,13 @@ abstract sealed class Lucene99MemorySegmentFloatVectorScorer
long offset4 = (long) nodes[i + 3] * vectorByteSize;
vectorOp(seg, scratchScores, offset1, offset2, offset3, offset4, query.length);
scores[i + 0] = normalizeRawScore(scratchScores[0]);
+ maxScore = Math.max(maxScore, scores[i + 0]);
scores[i + 1] = normalizeRawScore(scratchScores[1]);
+ maxScore = Math.max(maxScore, scores[i + 1]);
scores[i + 2] = normalizeRawScore(scratchScores[2]);
+ maxScore = Math.max(maxScore, scores[i + 2]);
scores[i + 3] = normalizeRawScore(scratchScores[3]);
+ maxScore = Math.max(maxScore, scores[i + 3]);
}
// Handle remaining 1–3 nodes in bulk (if any)
int remaining = numNodes - i;
@@ -103,9 +108,17 @@ abstract sealed class Lucene99MemorySegmentFloatVectorScorer
long addr3 = (remaining > 2) ? (long) nodes[i + 2] * vectorByteSize : addr1;
vectorOp(seg, scratchScores, addr1, addr2, addr3, addr3, query.length);
scores[i] = normalizeRawScore(scratchScores[0]);
- if (remaining > 1) scores[i + 1] = normalizeRawScore(scratchScores[1]);
- if (remaining > 2) scores[i + 2] = normalizeRawScore(scratchScores[2]);
+ maxScore = Math.max(maxScore, scores[i]);
+ if (remaining > 1) {
+ scores[i + 1] = normalizeRawScore(scratchScores[1]);
+ maxScore = Math.max(maxScore, scores[i + 1]);
+ }
+ if (remaining > 2) {
+ scores[i + 2] = normalizeRawScore(scratchScores[2]);
+ maxScore = Math.max(maxScore, scores[i + 1]);
+ }
}
+ return maxScore;
}
abstract void vectorOp(
diff --git a/lucene/core/src/java21/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorerSupplier.java b/lucene/core/src/java21/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorerSupplier.java
index 831abd41fd9..802740d3a0e 100644
--- a/lucene/core/src/java21/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorerSupplier.java
+++ b/lucene/core/src/java21/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorerSupplier.java
@@ -290,9 +290,10 @@ public abstract sealed class Lucene99MemorySegmentFloatVectorScorerSupplier
}
@Override
- public void bulkScore(int[] nodes, float[] scores, int numNodes) {
+ public float bulkScore(int[] nodes, float[] scores, int numNodes) {
int i = 0;
long queryAddr = (long) queryOrd * vectorByteSize;
+ float maxScore = Float.NEGATIVE_INFINITY;
final int limit = numNodes & ~3;
for (; i < limit; i += 4) {
long offset1 = (long) nodes[i] * vectorByteSize;
@@ -301,9 +302,13 @@ public abstract sealed class Lucene99MemorySegmentFloatVectorScorerSupplier
long offset4 = (long) nodes[i + 3] * vectorByteSize;
vectorOp(seg, scratchScores, queryAddr, offset1, offset2, offset3, offset4, dims);
scores[i + 0] = normalizeRawScore(scratchScores[0]);
+ maxScore = Math.max(maxScore, scores[i + 0]);
scores[i + 1] = normalizeRawScore(scratchScores[1]);
+ maxScore = Math.max(maxScore, scores[i + 1]);
scores[i + 2] = normalizeRawScore(scratchScores[2]);
+ maxScore = Math.max(maxScore, scores[i + 2]);
scores[i + 3] = normalizeRawScore(scratchScores[3]);
+ maxScore = Math.max(maxScore, scores[i + 3]);
}
// Handle remaining 1–3 nodes in bulk (if any)
int remaining = numNodes - i;
@@ -313,9 +318,17 @@ public abstract sealed class Lucene99MemorySegmentFloatVectorScorerSupplier
long addr3 = (remaining > 2) ? (long) nodes[i + 2] * vectorByteSize : addr1;
vectorOp(seg, scratchScores, queryAddr, addr1, addr2, addr3, addr1, dims);
scores[i] = normalizeRawScore(scratchScores[0]);
- if (remaining > 1) scores[i + 1] = normalizeRawScore(scratchScores[1]);
- if (remaining > 2) scores[i + 2] = normalizeRawScore(scratchScores[2]);
+ maxScore = Math.max(maxScore, scores[i]);
+ if (remaining > 1) {
+ scores[i + 1] = normalizeRawScore(scratchScores[1]);
+ maxScore = Math.max(maxScore, scores[i + 1]);
+ }
+ if (remaining > 2) {
+ scores[i + 2] = normalizeRawScore(scratchScores[2]);
+ maxScore = Math.max(maxScore, scores[i + 2]);
+ }
}
+ return maxScore;
}
@Override
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/hnsw/TestFlatVectorScorer.java b/lucene/core/src/test/org/apache/lucene/codecs/hnsw/TestFlatVectorScorer.java
index 847c7c2479f..c6707b18816 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/hnsw/TestFlatVectorScorer.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/hnsw/TestFlatVectorScorer.java
@@ -259,11 +259,13 @@ public class TestFlatVectorScorer extends BaseVectorizationTestCase {
: flatVectorsScorer.getRandomVectorScorer(sim, values, randomFloatVector(dims));
int[] indices = randomIndices(size);
float[] expectedScores = new float[size];
+ float expectedMaxScore = Float.NEGATIVE_INFINITY;
for (int i = 0; i < size; i++) {
expectedScores[i] = scorer.score(indices[i]);
+ expectedMaxScore = Math.max(expectedMaxScore, expectedScores[i]);
}
float[] bulkScores = new float[size];
- scorer.bulkScore(indices, bulkScores, size);
+ assertEquals(expectedMaxScore, scorer.bulkScore(indices, bulkScores, size), 0.001);
assertArrayEquals(expectedScores, bulkScores, delta);
assertNoScoreBeyondNumNodes(scorer, size);
}
@@ -301,8 +303,10 @@ public class TestFlatVectorScorer extends BaseVectorizationTestCase {
DefaultFlatVectorScorer.INSTANCE.getRandomVectorScorerSupplier(sim, values).scorer();
defaultScorer.setScoringOrdinal(targetNode);
float[] expectedScores = new float[size];
+ float expectedMaxScore = Float.NEGATIVE_INFINITY;
for (int i = 0; i < size; i++) {
expectedScores[i] = defaultScorer.score(indices[i]);
+ expectedMaxScore = Math.max(expectedMaxScore, expectedScores[i]);
}
var supplier = flatVectorsScorer.getRandomVectorScorerSupplier(sim, values);
@@ -310,7 +314,7 @@ public class TestFlatVectorScorer extends BaseVectorizationTestCase {
var updatableScorer = ss.scorer();
updatableScorer.setScoringOrdinal(targetNode);
float[] bulkScores = new float[size];
- updatableScorer.bulkScore(indices, bulkScores, size);
+ assertEquals(expectedMaxScore, updatableScorer.bulkScore(indices, bulkScores, size), 0.001);
assertArrayEquals(expectedScores, bulkScores, delta);
}
}