|
@@ -19,15 +19,18 @@
|
|
|
|
|
|
package org.elasticsearch.analysis.common;
|
|
|
|
|
|
+import org.apache.lucene.analysis.Tokenizer;
|
|
|
import org.elasticsearch.Version;
|
|
|
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.env.Environment;
|
|
|
+import org.elasticsearch.index.analysis.TokenizerFactory;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
import org.elasticsearch.test.IndexSettingsModule;
|
|
|
import org.elasticsearch.test.VersionUtils;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
public class CommonAnalysisPluginTests extends ESTestCase {
|
|
|
|
|
@@ -102,4 +105,82 @@ public class CommonAnalysisPluginTests extends ESTestCase {
|
|
|
+ "Please change the filter name to [edge_ngram] instead.");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Check that we log a deprecation warning for "nGram" and "edgeNGram" tokenizer names with 7.6 and
|
|
|
+ * disallow usages for indices created after 8.0
|
|
|
+ */
|
|
|
+ public void testNGramTokenizerDeprecation() throws IOException {
|
|
|
+ // tests for prebuilt tokenizer
|
|
|
+ doTestPrebuiltTokenizerDeprecation("nGram", "ngram",
|
|
|
+ VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false);
|
|
|
+ doTestPrebuiltTokenizerDeprecation("edgeNGram", "edge_ngram",
|
|
|
+ VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false);
|
|
|
+ doTestPrebuiltTokenizerDeprecation("nGram", "ngram",
|
|
|
+ VersionUtils.randomVersionBetween(random(), Version.V_7_6_0,
|
|
|
+ Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0))),
|
|
|
+ true);
|
|
|
+ doTestPrebuiltTokenizerDeprecation("edgeNGram", "edge_ngram",
|
|
|
+ VersionUtils.randomVersionBetween(random(), Version.V_7_6_0,
|
|
|
+ Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0))), true);
|
|
|
+ expectThrows(IllegalArgumentException.class, () -> doTestPrebuiltTokenizerDeprecation("nGram", "ngram",
|
|
|
+ VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), true));
|
|
|
+ expectThrows(IllegalArgumentException.class, () -> doTestPrebuiltTokenizerDeprecation("edgeNGram", "edge_ngram",
|
|
|
+ VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), true));
|
|
|
+
|
|
|
+ // same batch of tests for custom tokenizer definition in the settings
|
|
|
+ doTestCustomTokenizerDeprecation("nGram", "ngram",
|
|
|
+ VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false);
|
|
|
+ doTestCustomTokenizerDeprecation("edgeNGram", "edge_ngram",
|
|
|
+ VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false);
|
|
|
+ doTestCustomTokenizerDeprecation("nGram", "ngram",
|
|
|
+ VersionUtils.randomVersionBetween(random(), Version.V_7_6_0,
|
|
|
+ Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0))),
|
|
|
+ true);
|
|
|
+ doTestCustomTokenizerDeprecation("edgeNGram", "edge_ngram",
|
|
|
+ VersionUtils.randomVersionBetween(random(), Version.V_7_6_0,
|
|
|
+ Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0))), true);
|
|
|
+ expectThrows(IllegalArgumentException.class, () -> doTestCustomTokenizerDeprecation("nGram", "ngram",
|
|
|
+ VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), true));
|
|
|
+ expectThrows(IllegalArgumentException.class, () -> doTestCustomTokenizerDeprecation("edgeNGram", "edge_ngram",
|
|
|
+ VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), true));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void doTestPrebuiltTokenizerDeprecation(String deprecatedName, String replacement, Version version, boolean expectWarning)
|
|
|
+ throws IOException {
|
|
|
+ final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
|
|
|
+ .put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
|
|
|
+
|
|
|
+ try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) {
|
|
|
+ Map<String, TokenizerFactory> tokenizers = createTestAnalysis(
|
|
|
+ IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin).tokenizer;
|
|
|
+ TokenizerFactory tokenizerFactory = tokenizers.get(deprecatedName);
|
|
|
+
|
|
|
+ Tokenizer tokenizer = tokenizerFactory.create();
|
|
|
+ assertNotNull(tokenizer);
|
|
|
+ if (expectWarning) {
|
|
|
+ assertWarnings("The [" + deprecatedName + "] tokenizer name is deprecated and will be removed in a future version. "
|
|
|
+ + "Please change the tokenizer name to [" + replacement + "] instead.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void doTestCustomTokenizerDeprecation(String deprecatedName, String replacement, Version version, boolean expectWarning)
|
|
|
+ throws IOException {
|
|
|
+ final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
|
|
|
+ .put(IndexMetaData.SETTING_VERSION_CREATED, version)
|
|
|
+ .put("index.analysis.analyzer.custom_analyzer.type", "custom")
|
|
|
+ .put("index.analysis.analyzer.custom_analyzer.tokenizer", "my_tokenizer")
|
|
|
+ .put("index.analysis.tokenizer.my_tokenizer.type", deprecatedName)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) {
|
|
|
+ createTestAnalysis(IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin);
|
|
|
+
|
|
|
+ if (expectWarning) {
|
|
|
+ assertWarnings("The [" + deprecatedName + "] tokenizer name is deprecated and will be removed in a future version. "
|
|
|
+ + "Please change the tokenizer name to [" + replacement + "] instead.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|