Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/124662.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 124662
summary: Let MLTQuery throw IAE when no analyzer is set
area: Search
type: bug
issues:
- 124562
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,7 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException {
// 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +42,7 @@
import org.junit.Before;

import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
Expand Down Expand Up @@ -393,6 +395,19 @@ public void testItemFromXContent() throws IOException {
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
*/
Expand Down