Skip to content

Commit d9f331f

Browse files
committed
Fix doc ord bug & flush writer multiple times
1 parent b67637a commit d9f331f

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lucene/core/src/java/org/apache/lucene/search/RerankKnnFloatVectorQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Query rewrite(IndexSearcher indexSearcher) throws IOException {
7373
int docId = iterator.docID();
7474
float[] vectorValue = floatVectorValues.vectorValue(docId);
7575
float score = comparer.compare(vectorValue, target);
76-
queue.insertWithOverflow(new ScoreDoc(docId, score));
76+
queue.insertWithOverflow(new ScoreDoc(leaf.docBase + docId, score));
7777
}
7878
}
7979
int i = 0;

lucene/core/src/test/org/apache/lucene/search/TestRerankKnnFloatVectorQuery.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.lucene.search;
1818

19+
import java.util.Arrays;
1920
import java.util.HashMap;
2021
import java.util.Map;
2122
import java.util.Random;
@@ -46,7 +47,6 @@ public class TestRerankKnnFloatVectorQuery extends LuceneTestCase {
4647
VectorSimilarityFunction.COSINE;
4748
private Directory directory;
4849
private IndexWriterConfig config;
49-
private static final int NUM_VECTORS = 1000;
5050
private static final int VECTOR_DIMENSION = 128;
5151

5252
@Before
@@ -66,15 +66,22 @@ public void testTwoPhaseKnnVectorQuery() throws Exception {
6666

6767
Random random = random();
6868

69+
int numVectors = atLeast(1000);
70+
6971
// Step 1: Index random vectors in quantized format
7072
try (IndexWriter writer = new IndexWriter(directory, config)) {
71-
for (int i = 0; i < NUM_VECTORS; i++) {
73+
for (int i = 0; i < numVectors; i++) {
7274
float[] vector = randomFloatVector(VECTOR_DIMENSION, random);
7375
Document doc = new Document();
7476
doc.add(new IntField("id", i, Field.Store.YES));
7577
doc.add(new KnnFloatVectorField(FIELD, vector, VECTOR_SIMILARITY_FUNCTION));
7678
writer.addDocument(doc);
7779
vectors.put(i, vector);
80+
81+
// flush to create multiple segments
82+
if (random.nextInt(10) == 0) {
83+
writer.flush();
84+
}
7885
}
7986
}
8087

@@ -93,10 +100,11 @@ public void testTwoPhaseKnnVectorQuery() throws Exception {
93100
// Step 3: Verify that TopDocs scores match similarity with unquantized vectors
94101
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
95102
Document retrievedDoc = searcher.storedFields().document(scoreDoc.doc);
96-
float[] docVector = vectors.get(retrievedDoc.getField("id").numericValue().intValue());
103+
int id = retrievedDoc.getField("id").numericValue().intValue();
104+
float[] docVector = vectors.get(id);
97105
float expectedScore = VECTOR_SIMILARITY_FUNCTION.compare(targetVector, docVector);
98106
Assert.assertEquals(
99-
"Score does not match expected similarity for docId: " + scoreDoc.doc,
107+
"Score does not match expected similarity for doc ord: " + scoreDoc.doc + ", id: " + id,
100108
expectedScore,
101109
scoreDoc.score,
102110
1e-5);

0 commit comments

Comments
 (0)