Skip to content

Commit 8cd3ccf

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

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-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: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public class TestRerankKnnFloatVectorQuery extends LuceneTestCase {
4646
VectorSimilarityFunction.COSINE;
4747
private Directory directory;
4848
private IndexWriterConfig config;
49-
private static final int NUM_VECTORS = 1000;
5049
private static final int VECTOR_DIMENSION = 128;
5150

5251
@Before
@@ -66,15 +65,22 @@ public void testTwoPhaseKnnVectorQuery() throws Exception {
6665

6766
Random random = random();
6867

68+
int numVectors = atLeast(1000);
69+
6970
// Step 1: Index random vectors in quantized format
7071
try (IndexWriter writer = new IndexWriter(directory, config)) {
71-
for (int i = 0; i < NUM_VECTORS; i++) {
72+
for (int i = 0; i < numVectors; i++) {
7273
float[] vector = randomFloatVector(VECTOR_DIMENSION, random);
7374
Document doc = new Document();
7475
doc.add(new IntField("id", i, Field.Store.YES));
7576
doc.add(new KnnFloatVectorField(FIELD, vector, VECTOR_SIMILARITY_FUNCTION));
7677
writer.addDocument(doc);
7778
vectors.put(i, vector);
79+
80+
// flush to create multiple segments
81+
if (random.nextInt(10) == 0) {
82+
writer.flush();
83+
}
7884
}
7985
}
8086

@@ -93,10 +99,11 @@ public void testTwoPhaseKnnVectorQuery() throws Exception {
9399
// Step 3: Verify that TopDocs scores match similarity with unquantized vectors
94100
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
95101
Document retrievedDoc = searcher.storedFields().document(scoreDoc.doc);
96-
float[] docVector = vectors.get(retrievedDoc.getField("id").numericValue().intValue());
102+
int id = retrievedDoc.getField("id").numericValue().intValue();
103+
float[] docVector = vectors.get(id);
97104
float expectedScore = VECTOR_SIMILARITY_FUNCTION.compare(targetVector, docVector);
98105
Assert.assertEquals(
99-
"Score does not match expected similarity for docId: " + scoreDoc.doc,
106+
"Score does not match expected similarity for doc ord: " + scoreDoc.doc + ", id: " + id,
100107
expectedScore,
101108
scoreDoc.score,
102109
1e-5);

0 commit comments

Comments
 (0)