Let MLTQuery throw IAE when no analyzer is set (#124662)

* Let MLTQuery throw IAE when no analyzer is set
This commit is contained in:
Tommaso Teofili 2025-03-12 18:37:31 +01:00 committed by GitHub
parent 44a3ac444f
commit c971d79a95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 3 deletions

View file

@ -0,0 +1,6 @@
pr: 124662
summary: Let MLTQuery throw IAE when no analyzer is set
area: Search
type: bug
issues:
- 124562

View file

@ -911,9 +911,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
// set analyzer
Analyzer analyzerObj = context.getIndexAnalyzers().get(analyzer);
if (analyzerObj == null) {
analyzerObj = context.getIndexAnalyzer(
f -> { throw new UnsupportedOperationException("No analyzer configured for field " + f); }
);
analyzerObj = context.getIndexAnalyzer(f -> { throw new IllegalArgumentException("No analyzer configured for field " + f); });
}
mltQuery.setAnalyzer(analyzer, analyzerObj);

View file

@ -9,6 +9,7 @@
package org.elasticsearch.index.query;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
@ -41,6 +42,7 @@ import org.elasticsearch.xcontent.json.JsonXContent;
import org.junit.Before;
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
@ -393,6 +395,19 @@ public class MoreLikeThisQueryBuilderTests extends AbstractQueryTestCase<MoreLik
assertEquals(expectedItem, newItem);
}
public void testNonExistingAnalyzer() throws IOException {
MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = moreLikeThisQuery(
new String[] { "name.first", "name.last" },
new String[] { "something" },
null
);
moreLikeThisQueryBuilder.analyzer("thisDoesntExist");
SearchExecutionContext searchExecutionContext = createSearchExecutionContext();
Query query = moreLikeThisQueryBuilder.toQuery(searchExecutionContext);
Analyzer analyzer = ((MoreLikeThisQuery) query).getAnalyzer();
assertThrows(IllegalArgumentException.class, () -> analyzer.tokenStream("thisDoesntExist", new StringReader("something")));
}
/**
* Check that this query is generally not cacheable, except when we fetch 0 items
*/