Fix flaky testToString method for Knn Vector queries (#12500)

Periodically, the random indexer will force merge on close, this means that what was originally indexed as the zeroth document could no longer be the zeroth document.

This commit adjusts the assertion to ensure the to string format is as expected for `DocAndScoreQuery`, regardless of the matching doc-id in the test.

This seed shows the issue:
```
./gradlew test --tests TestKnnByteVectorQuery.testToString -Dtests.seed=B78CDB966F4B8FC5
```
This commit is contained in:
Benjamin Trent 2023-08-11 07:26:49 -04:00 committed by GitHub
parent 13e747f95f
commit df8745e59e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 4 deletions

View File

@ -792,6 +792,15 @@ abstract class BaseKnnVectorQueryTestCase extends LuceneTestCase {
assertEquals(expectedId, actualId);
}
void assertDocScoreQueryToString(Query query) {
String queryString = query.toString("ignored");
// The string should contain matching docIds and their score.
// Since a forceMerge could occur in this test, we must not assert that a specific doc_id is
// matched
// But that instead the string format is expected and that the score is 1.0
assertTrue(queryString.matches("DocAndScoreQuery\\[\\d+,...]\\[1.0,...]"));
}
/**
* A version of {@link AbstractKnnVectorQuery} that throws an error when an exact search is run.
* This allows us to check what search strategy is being used.

View File

@ -76,8 +76,7 @@ public class TestKnnByteVectorQuery extends BaseKnnVectorQueryTestCase {
AbstractKnnVectorQuery query = getKnnVectorQuery("field", new float[] {0, 1}, 10);
assertEquals("KnnByteVectorQuery:field[0,...][10]", query.toString("ignored"));
Query rewritten = query.rewrite(newSearcher(reader));
assertEquals("DocAndScoreQuery[0,...][1.0,...]", rewritten.toString("ignored"));
assertDocScoreQueryToString(query.rewrite(newSearcher(reader)));
}
}

View File

@ -75,8 +75,7 @@ public class TestKnnFloatVectorQuery extends BaseKnnVectorQueryTestCase {
AbstractKnnVectorQuery query = getKnnVectorQuery("field", new float[] {0.0f, 1.0f}, 10);
assertEquals("KnnFloatVectorQuery:field[0.0,...][10]", query.toString("ignored"));
Query rewritten = query.rewrite(newSearcher(reader));
assertEquals("DocAndScoreQuery[0,...][1.0,...]", rewritten.toString("ignored"));
assertDocScoreQueryToString(query.rewrite(newSearcher(reader)));
}
}