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:16:52 UTC
(lucene) branch main 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 main
in repository https://gitbox.apache.org/repos/asf/lucene.git
The following commit(s) were added to refs/heads/main by this push:
new 0421a8de771 integrate into new bulk scorers (#15021)
0421a8de771 is described below
commit 0421a8de771ed7e38f27957c78e788deb52db0ca
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 cf2856023e9..fbe85229fc5 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
@@ -361,10 +361,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;
}
@@ -372,10 +373,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/java24/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorer.java b/lucene/core/src/java24/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorer.java
index 96d84b40bc9..b5f1e485df6 100644
--- a/lucene/core/src/java24/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorer.java
+++ b/lucene/core/src/java24/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorer.java
@@ -80,9 +80,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;
@@ -90,9 +91,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;
@@ -102,9 +107,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/java24/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorerSupplier.java b/lucene/core/src/java24/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorerSupplier.java
index 48290fb7abb..01ba6a05c80 100644
--- a/lucene/core/src/java24/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorerSupplier.java
+++ b/lucene/core/src/java24/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorerSupplier.java
@@ -289,9 +289,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;
@@ -300,9 +301,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;
@@ -312,9 +317,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 dada4a7d5a6..4bd2a06ec18 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
@@ -260,11 +260,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);
}
@@ -302,8 +304,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);
@@ -311,7 +315,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);
}
}