Skip to content

Commit 13803aa

Browse files
benwtrentjavanna
authored andcommitted
Fix flaky TestHnswByteVectorGraph.testSortedAndUnsortedIndicesReturnSameResults test (#12110)
1 parent 6ac4e0b commit 13803aa

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

lucene/core/src/test/org/apache/lucene/util/hnsw/HnswGraphTestCase.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -215,26 +215,32 @@ public KnnVectorsFormat getKnnVectorsFormatForField(String field) {
215215
// ask to explore a lot of candidates to ensure the same returned hits,
216216
// as graphs of 2 indices are organized differently
217217
Query query = knnQuery("vector", randomVector(dim), 50);
218-
List<String> ids1 = new ArrayList<>();
219-
List<Integer> docs1 = new ArrayList<>();
220-
List<String> ids2 = new ArrayList<>();
221-
List<Integer> docs2 = new ArrayList<>();
222-
223-
TopDocs topDocs = searcher.search(query, 5);
218+
int searchSize = 5;
219+
List<String> ids1 = new ArrayList<>(searchSize);
220+
List<Integer> docs1 = new ArrayList<>(searchSize);
221+
List<String> ids2 = new ArrayList<>(searchSize);
222+
List<Integer> docs2 = new ArrayList<>(searchSize);
223+
224+
// Check if a duplicate score exists in n+1, if so, this test is invalid
225+
// Else, continue to fail on ID equality as this test failed
226+
TopDocs topDocs = searcher.search(query, searchSize + 1);
224227
float lastScore = -1;
225228
StoredFields storedFields = reader.storedFields();
226-
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
229+
for (int j = 0; j < searchSize + 1; j++) {
230+
ScoreDoc scoreDoc = topDocs.scoreDocs[j];
227231
if (scoreDoc.score == lastScore) {
228232
// if we have repeated score this test is invalid
229233
continue OUTER;
230234
} else {
231235
lastScore = scoreDoc.score;
232236
}
233-
Document doc = storedFields.document(scoreDoc.doc, Set.of("id"));
234-
ids1.add(doc.get("id"));
235-
docs1.add(scoreDoc.doc);
237+
if (j < searchSize) {
238+
Document doc = storedFields.document(scoreDoc.doc, Set.of("id"));
239+
ids1.add(doc.get("id"));
240+
docs1.add(scoreDoc.doc);
241+
}
236242
}
237-
TopDocs topDocs2 = searcher2.search(query, 5);
243+
TopDocs topDocs2 = searcher2.search(query, searchSize);
238244
StoredFields storedFields2 = reader2.storedFields();
239245
for (ScoreDoc scoreDoc : topDocs2.scoreDocs) {
240246
Document doc = storedFields2.document(scoreDoc.doc, Set.of("id"));

0 commit comments

Comments
 (0)