Guard against a null scorer in painless execute (#109048)

The painless execute api allows for a sample document and query to be
provided. If the query does not actually match the document, a null
scorer is produced. This commit guards against that condition, returning
a clearer error message indicating what happened.

closes #43541
This commit is contained in:
Ryan Ernst 2024-05-29 15:30:02 -07:00 committed by GitHub
parent b8894c39ed
commit db1c76bef5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 0 deletions

View file

@ -0,0 +1,6 @@
pr: 109048
summary: Guard against a null scorer in painless execute
area: Infra/Scripting
type: bug
issues:
- 43541

View file

@ -648,6 +648,9 @@ public class PainlessExecuteAction {
luceneQuery = indexSearcher.rewrite(luceneQuery); luceneQuery = indexSearcher.rewrite(luceneQuery);
Weight weight = indexSearcher.createWeight(luceneQuery, ScoreMode.COMPLETE, 1f); Weight weight = indexSearcher.createWeight(luceneQuery, ScoreMode.COMPLETE, 1f);
Scorer scorer = weight.scorer(indexSearcher.getIndexReader().leaves().get(0)); Scorer scorer = weight.scorer(indexSearcher.getIndexReader().leaves().get(0));
if (scorer == null) {
throw new IllegalArgumentException("The provided query did not match the sample document");
}
// Consume the first (and only) match. // Consume the first (and only) match.
int docID = scorer.iterator().nextDoc(); int docID = scorer.iterator().nextDoc();
assert docID == scorer.docID(); assert docID == scorer.docID();