LUCENE-10063 Correct BaseKnnVectorsFormatTestCase.testRandomWithUpdatesAndGraph (#278)

- Make sure that k > 0 for knn search
- Make sure that k doesn't exceed the number of live docs

Relates to #262
This commit is contained in:
Mayya Sharipova 2021-09-02 16:23:31 -04:00 committed by GitHub
parent eb2509c846
commit 54179e9372
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 3 deletions

View File

@ -842,6 +842,7 @@ public abstract class BaseKnnVectorsFormatTestCase extends BaseIndexFileFormatTe
continue;
}
int docId;
int numLiveDocsWithVectors = 0;
while ((docId = vectorValues.nextDoc()) != NO_MORE_DOCS) {
float[] v = vectorValues.vectorValue();
assertEquals(dimension, v.length);
@ -853,16 +854,25 @@ public abstract class BaseKnnVectorsFormatTestCase extends BaseIndexFileFormatTe
id2value[id],
v,
0);
numLiveDocsWithVectors++;
} else {
if (id2value[id] != null) {
assertFalse(Arrays.equals(id2value[id], v));
}
}
}
// assert that searchNearestVectors returns the expected number of documents, in
// descending score order
if (numLiveDocsWithVectors == 0) {
continue;
}
// assert that searchNearestVectors returns the expected number of documents,
// in descending score order
int size = ctx.reader().getVectorValues(fieldName).size();
int k = random().nextInt(size / 2 + 1);
int k = random().nextInt(size / 2 + 1) + 1;
if (k > numLiveDocsWithVectors) {
k = numLiveDocsWithVectors;
}
TopDocs results =
ctx.reader().searchNearestVectors(fieldName, randomVector(dimension), k, liveDocs);
assertEquals(Math.min(k, size), results.scoreDocs.length);