1616 */
1717package org .apache .lucene .search ;
1818
19+ import java .util .Arrays ;
1920import java .util .HashMap ;
2021import java .util .Map ;
2122import 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