Skip to content
Closed
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Once your changes and tests are ready to submit for review:
1. Test your changes

Run the test suite to make sure that nothing is broken. See the
[TESTING](TESTING.asciidoc) file for help running tests.
[TESTING](TESTING.asciidoc) file for helping run tests.

2. Sign the Contributor License Agreement

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 unsupported 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 [my_analyzer] does not support [foo]" }
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public final class AnalysisRegistry implements Closeable {
private final Map<String, AnalysisProvider<TokenizerFactory>> tokenizers;
private final Map<String, AnalysisProvider<AnalyzerProvider<?>>> analyzers;
private final Map<String, AnalysisProvider<AnalyzerProvider<?>>> normalizers;
private static final Version CUSTOM_ANALYZER_KEY_CHECK_VERSION = Version.V_8_5_0;

public AnalysisRegistry(
Environment environment,
Expand Down Expand Up @@ -459,6 +460,14 @@ private <T> Map<String, T> buildMapping(
Settings currentSettings = entry.getValue();
String typeName = currentSettings.get("type");
if (component == Component.ANALYZER) {
if (settings.getIndexVersionCreated().onOrAfter(CUSTOM_ANALYZER_KEY_CHECK_VERSION)) {
for (String key : currentSettings.keySet()) {
switch (key) {
case "tokenizer", "char_filter", "filter", "type", "position_increment_gap" -> {}
default -> throw new IllegalArgumentException("Custom analyzer [" + name + "] does not support [" + key + "]");
}
}
}
T factory = null;
if (typeName == null) {
if (currentSettings.get("tokenizer") != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.IndexSettings;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.IndexSettingsModule;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
Expand All @@ -27,6 +31,7 @@
import java.util.Arrays;
import java.util.List;

import static java.util.Collections.emptyMap;
import static org.hamcrest.Matchers.is;

public class AnalysisTests extends ESTestCase {
Expand Down Expand Up @@ -103,4 +108,36 @@ public void testParseWordList() throws IOException {
List<String> wordList = Analysis.getWordList(env, nodeSettings, "foo.bar");
assertEquals(Arrays.asList("hello", "world"), wordList);
}

public void testCustomAnalyzerWithUnsupportedKey() {
Settings analyzerSettings = Settings.builder().
put("index.analysis.analyzer.my_analyzer.tokenizer", "standard").
put("index.analysis.analyzer.my_analyzer.type", "custom").
put("index.analysis.analyzer.my_analyzer.foo", "bar").
build();
Settings settings = Settings.builder().
put(IndexMetadata.SETTING_VERSION_CREATED, Version.V_8_5_0).
put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).
put(analyzerSettings).
build();

IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings);
AnalysisRegistry emptyAnalysisRegistry = new AnalysisRegistry(
TestEnvironment.newEnvironment(settings),
emptyMap(),
emptyMap(),
emptyMap(),
emptyMap(),
emptyMap(),
emptyMap(),
emptyMap(),
emptyMap(),
emptyMap()
);
IllegalArgumentException ex = expectThrows(
IllegalArgumentException.class,
() -> emptyAnalysisRegistry.build(idxSettings)
);
assertEquals(ex.getMessage(), "Custom analyzer [my_analyzer] does not support [foo]");
}
}