mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-06-28 17:34:17 -04:00
Let MLTQuery throw IAE when no analyzer is set (#124662)
* Let MLTQuery throw IAE when no analyzer is set
This commit is contained in:
parent
44a3ac444f
commit
c971d79a95
3 changed files with 22 additions and 3 deletions
6
docs/changelog/124662.yaml
Normal file
6
docs/changelog/124662.yaml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
pr: 124662
|
||||||
|
summary: Let MLTQuery throw IAE when no analyzer is set
|
||||||
|
area: Search
|
||||||
|
type: bug
|
||||||
|
issues:
|
||||||
|
- 124562
|
|
@ -911,9 +911,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
|
||||||
// set analyzer
|
// set analyzer
|
||||||
Analyzer analyzerObj = context.getIndexAnalyzers().get(analyzer);
|
Analyzer analyzerObj = context.getIndexAnalyzers().get(analyzer);
|
||||||
if (analyzerObj == null) {
|
if (analyzerObj == null) {
|
||||||
analyzerObj = context.getIndexAnalyzer(
|
analyzerObj = context.getIndexAnalyzer(f -> { throw new IllegalArgumentException("No analyzer configured for field " + f); });
|
||||||
f -> { throw new UnsupportedOperationException("No analyzer configured for field " + f); }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
mltQuery.setAnalyzer(analyzer, analyzerObj);
|
mltQuery.setAnalyzer(analyzer, analyzerObj);
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
package org.elasticsearch.index.query;
|
package org.elasticsearch.index.query;
|
||||||
|
|
||||||
|
import org.apache.lucene.analysis.Analyzer;
|
||||||
import org.apache.lucene.analysis.TokenStream;
|
import org.apache.lucene.analysis.TokenStream;
|
||||||
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
|
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
|
||||||
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
||||||
|
@ -41,6 +42,7 @@ import org.elasticsearch.xcontent.json.JsonXContent;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
|
@ -393,6 +395,19 @@ public class MoreLikeThisQueryBuilderTests extends AbstractQueryTestCase<MoreLik
|
||||||
assertEquals(expectedItem, newItem);
|
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
|
* Check that this query is generally not cacheable, except when we fetch 0 items
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue