Skip to content

Commit 2ffd44b

Browse files
authored
8.x branch backport for leting MLTQuery throw IAE when no analyzer is set (#124662) (#125192)
* Let MLTQuery throw IAE when no analyzer is set (#124662) (cherry picked from commit c971d79)
1 parent 0196d11 commit 2ffd44b

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-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

docs/changelog/125192.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 125192
2+
summary: 8.x branch backport for leting MLTQuery throw IAE when no analyzer is set
3+
area: Search
4+
type: bug
5+
issues: []

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -969,9 +969,7 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException {
969969
// set analyzer
970970
Analyzer analyzerObj = context.getIndexAnalyzers().get(analyzer);
971971
if (analyzerObj == null) {
972-
analyzerObj = context.getIndexAnalyzer(
973-
f -> { throw new UnsupportedOperationException("No analyzer configured for field " + f); }
974-
);
972+
analyzerObj = context.getIndexAnalyzer(f -> { throw new IllegalArgumentException("No analyzer configured for field " + f); });
975973
}
976974
mltQuery.setAnalyzer(analyzer, analyzerObj);
977975

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)