-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Custom analyzer to reject unknown parameters #89112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
91dacc3
250959d
ea7106c
90ba7bf
0ed1304
394b2fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| pr: 89112 | ||
| summary: Make param check when create custom analyzer | ||
| area: Search/Analysis | ||
| type: enhancement | ||
| issues: | ||
| - 85710 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,3 +59,22 @@ | |
| - match: { detail.tokenizer.tokens.0.token: ABc } | ||
| - match: { detail.tokenfilters.0.name: lowercase } | ||
| - match: { detail.tokenfilters.0.tokens.0.token: abc } | ||
|
|
||
| --- | ||
| "Custom analyzer with not supported param in request": | ||
| - do: | ||
| catch: bad_request | ||
| indices.create: | ||
| index: test | ||
| body: | ||
| settings: | ||
| analysis: | ||
| analyzer: | ||
| my_analyzer: | ||
| type: custom | ||
| tokenizer: standard | ||
| foo: bar | ||
|
|
||
| - match: { status: 400 } | ||
| - match: { error.type: illegal_argument_exception } | ||
| - match: { error.reason: "Custom Analyzer not support [foo] now" } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,6 @@ | |
| tokenizer: standard | ||
| filter: [lowercase] | ||
| search: | ||
| rest_total_hits_as_int: true | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why was this change necessary? |
||
| tokenizer: standard | ||
| filter: [lowercase, keyword_repeat, porter_stem, unique_stem] | ||
| filter: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,12 @@ static AnalyzerComponents createComponents( | |
| final Map<String, CharFilterFactory> charFilters, | ||
| final Map<String, TokenFilterFactory> tokenFilters | ||
| ) { | ||
| for (String key : analyzerSettings.keySet()) { | ||
|
||
| switch (key) { | ||
| case "tokenizer", "char_filter", "filter", "type", "position_increment_gap" -> {} | ||
|
||
| default -> throw new IllegalArgumentException("Custom Analyzer not support [" + key + "] now"); | ||
|
||
| } | ||
| } | ||
| String tokenizerName = analyzerSettings.get("tokenizer"); | ||
| if (tokenizerName == null) { | ||
| throw new IllegalArgumentException("Custom Analyzer [" + name + "] must be configured with a tokenizer"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,10 +9,14 @@ | |
| package org.elasticsearch.index.analysis; | ||
|
|
||
| import org.apache.lucene.analysis.CharArraySet; | ||
| import org.elasticsearch.Version; | ||
| import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.env.Environment; | ||
| import org.elasticsearch.env.TestEnvironment; | ||
| import org.elasticsearch.index.Index; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.junit.BeforeClass; | ||
|
|
||
| import java.io.BufferedWriter; | ||
| import java.io.FileNotFoundException; | ||
|
|
@@ -27,9 +31,18 @@ | |
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import static org.elasticsearch.index.analysis.AnalyzerComponents.createComponents; | ||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
| public class AnalysisTests extends ESTestCase { | ||
| private static TestAnalysis testAnalysis; | ||
| private static Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build(); | ||
|
|
||
| @BeforeClass | ||
| public static void setup() throws IOException { | ||
| testAnalysis = createTestAnalysis(new Index("test", "_na_"), settings); | ||
| } | ||
|
||
|
|
||
| public void testParseStemExclusion() { | ||
| /* Comma separated list */ | ||
| Settings settings = Settings.builder().put("stem_exclusion", "foo,bar").build(); | ||
|
|
@@ -103,4 +116,15 @@ public void testParseWordList() throws IOException { | |
| List<String> wordList = Analysis.getWordList(env, nodeSettings, "foo.bar"); | ||
| assertEquals(Arrays.asList("hello", "world"), wordList); | ||
| } | ||
|
|
||
| public void testCustomAnalyzerWithNotSupportKey() { | ||
|
||
| Settings analyzerSettings = Settings.builder().put("tokenizer", "standard").put("foo", "bar").put("type", "custom").build(); | ||
|
|
||
| try { | ||
| createComponents("my_analyzer", analyzerSettings, testAnalysis.tokenizer, testAnalysis.charFilter, testAnalysis.tokenFilter); | ||
| fail("expected failure"); | ||
| } catch (IllegalArgumentException e) { | ||
|
||
| assertEquals(e.getMessage(), "Custom Analyzer not support [foo] now"); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the changelog generation is automatic in most situations, no need to create the entry yourself. And it gets updated as the labels and title of the PR get updated. Could you remove the changelog from your PR, then it should get automatically created with the expected area and all other fields. The current build errors are around the wrong area.