@@ -457,10 +457,12 @@ public void testLuceneKnnIndex_multipleMerges_with_ordering_check() throws IOExc
457457 final int baseDocId = context .docBase ;
458458 final FloatVectorValues vectorValues = context .reader ().getFloatVectorValues ("vec" );
459459 final int k = 1 ;
460- for (int docId = 0 ; docId < reader .maxDoc (); docId ++) {
461- float [] query = new float [] { docId , 0 } ;
460+ for (int i = 0 ; i < reader .maxDoc (); i ++) {
461+ float [] query = TestUtils . generateRandomVectors ( 1 , 2 )[ 0 ] ;
462462 TopDocs td = searcher .search (getJVectorKnnFloatVectorQuery ("vec" , query , k , new MatchAllDocsQuery ()), k );
463463 assertEquals (k , td .scoreDocs .length );
464+
465+ compareSearchResults (td , sourceVectors , reader , expectedDocIdField , baseDocId , vectorValues );
464466 }
465467
466468 // (c) search with the same vector and this time add concurrency to make sure we are still not exhausting the file handles
@@ -482,20 +484,7 @@ public void testLuceneKnnIndex_multipleMerges_with_ordering_check() throws IOExc
482484 try {
483485 TopDocs td = searcher .search (new KnnFloatVectorQuery ("vec" , query , k ), k );
484486 assertEquals ("Search should return correct number of results" , k , td .scoreDocs .length );
485- final int localDocId = td .scoreDocs [0 ].doc ;
486- final int globalDocId = reader .storedFields ()
487- .document (localDocId )
488- .getField (expectedDocIdField )
489- .storedValue ()
490- .getIntValue ();
491- float [] vectorValue = vectorValues .vectorValue (localDocId - baseDocId );
492- float [] expectedVectorValue = sourceVectors [globalDocId ];
493- Assert .assertArrayEquals (
494- "vectors in source and index should match" ,
495- expectedVectorValue ,
496- vectorValue ,
497- 0.0f
498- );
487+ compareSearchResults (td , sourceVectors , reader , expectedDocIdField , baseDocId , vectorValues );
499488 totalQueries .incrementAndGet ();
500489 } catch (Throwable e ) {
501490 failureDetected .compareAndSet (false , true );
@@ -527,6 +516,28 @@ public void testLuceneKnnIndex_multipleMerges_with_ordering_check() throws IOExc
527516
528517 }
529518
519+ private void compareSearchResults (
520+ TopDocs topDocs ,
521+ float [][] sourceVectors ,
522+ DirectoryReader reader ,
523+ String expectedDocIdField ,
524+ int baseDocId ,
525+ FloatVectorValues vectorValues
526+ ) throws IOException {
527+ for (int resultIdx = 0 ; resultIdx < topDocs .scoreDocs .length ; resultIdx ++) {
528+ final int localDocId = topDocs .scoreDocs [resultIdx ].doc ;
529+ final int globalDocId = reader .storedFields ().document (localDocId ).getField (expectedDocIdField ).storedValue ().getIntValue ();
530+
531+ // Access to float values is not thread safe
532+ final float [] vectorValue ;
533+ synchronized (vectorValues ) {
534+ vectorValue = vectorValues .vectorValue (localDocId - baseDocId );
535+ }
536+ float [] expectedVectorValue = sourceVectors [globalDocId ];
537+ Assert .assertArrayEquals ("vectors in source and index should match" , expectedVectorValue , vectorValue , 0.0f );
538+ }
539+ }
540+
530541 /**
531542 * Test to verify that a document which has been deleted is no longer
532543 * returned in a k-NN search. The index uses the JVector codec and is
0 commit comments