Skip to content

Commit e2a2564

Browse files
authored
Let MLTQuery throw IAE when no analyzer is set (#124662) (#125190)
* Let MLTQuery throw IAE when no analyzer is set (cherry picked from commit c971d79)
1 parent 595186a commit e2a2564

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

docs/changelog/124662.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 124662
2+
summary: Let MLTQuery throw IAE when no analyzer is set
3+
area: Search
4+
type: bug
5+
issues:
6+
- 124562

server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -911,9 +911,7 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException {
911911
// set analyzer
912912
Analyzer analyzerObj = context.getIndexAnalyzers().get(analyzer);
913913
if (analyzerObj == null) {
914-
analyzerObj = context.getIndexAnalyzer(
915-
f -> { throw new UnsupportedOperationException("No analyzer configured for field " + f); }
916-
);
914+
analyzerObj = context.getIndexAnalyzer(f -> { throw new IllegalArgumentException("No analyzer configured for field " + f); });
917915
}
918916
mltQuery.setAnalyzer(analyzer, analyzerObj);
919917

server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.index.query;
1111

12+
import org.apache.lucene.analysis.Analyzer;
1213
import org.apache.lucene.analysis.TokenStream;
1314
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
1415
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
@@ -41,6 +42,7 @@
4142
import org.junit.Before;
4243

4344
import java.io.IOException;
45+
import java.io.StringReader;
4446
import java.util.Arrays;
4547
import java.util.Collections;
4648
import java.util.EnumSet;
@@ -393,6 +395,19 @@ public void testItemFromXContent() throws IOException {
393395
assertEquals(expectedItem, newItem);
394396
}
395397

398+
public void testNonExistingAnalyzer() throws IOException {
399+
MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = moreLikeThisQuery(
400+
new String[] { "name.first", "name.last" },
401+
new String[] { "something" },
402+
null
403+
);
404+
moreLikeThisQueryBuilder.analyzer("thisDoesntExist");
405+
SearchExecutionContext searchExecutionContext = createSearchExecutionContext();
406+
Query query = moreLikeThisQueryBuilder.toQuery(searchExecutionContext);
407+
Analyzer analyzer = ((MoreLikeThisQuery) query).getAnalyzer();
408+
assertThrows(IllegalArgumentException.class, () -> analyzer.tokenStream("thisDoesntExist", new StringReader("something")));
409+
}
410+
396411
/**
397412
* Check that this query is generally not cacheable, except when we fetch 0 items
398413
*/

0 commit comments

Comments
 (0)