|
20 | 20 | package org.elasticsearch.analysis.common;
|
21 | 21 |
|
22 | 22 | import org.apache.lucene.analysis.Analyzer;
|
| 23 | +import org.apache.lucene.analysis.Tokenizer; |
23 | 24 | import org.elasticsearch.Version;
|
24 | 25 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
25 | 26 | import org.elasticsearch.common.settings.Settings;
|
26 | 27 | import org.elasticsearch.env.Environment;
|
27 | 28 | import org.elasticsearch.index.IndexSettings;
|
28 | 29 | import org.elasticsearch.index.analysis.IndexAnalyzers;
|
29 | 30 | import org.elasticsearch.index.analysis.NamedAnalyzer;
|
| 31 | +import org.elasticsearch.index.analysis.TokenizerFactory; |
30 | 32 | import org.elasticsearch.test.ESTestCase;
|
31 | 33 | import org.elasticsearch.test.IndexSettingsModule;
|
32 | 34 | import org.elasticsearch.test.VersionUtils;
|
33 | 35 |
|
34 | 36 | import java.io.IOException;
|
| 37 | +import java.util.Map; |
35 | 38 |
|
36 | 39 | public class CommonAnalysisPluginTests extends ESTestCase {
|
37 | 40 |
|
@@ -192,4 +195,64 @@ public void testEdgeNGramFilterInCustomAnalyzerDeprecationError() throws IOExcep
|
192 | 195 | assertWarnings("The [edgeNGram] token filter name is deprecated and will be removed in a future version. "
|
193 | 196 | + "Please change the filter name to [edge_ngram] instead.");
|
194 | 197 | }
|
| 198 | + |
| 199 | + /** |
| 200 | + * Check that we log a deprecation warning for "nGram" and "edgeNGram" tokenizer names with 7.6 and |
| 201 | + * disallow usages for indices created after 8.0 |
| 202 | + */ |
| 203 | + public void testNGramTokenizerDeprecation() throws IOException { |
| 204 | + // tests for prebuilt tokenizer |
| 205 | + doTestPrebuiltTokenizerDeprecation("nGram", "ngram", |
| 206 | + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false); |
| 207 | + doTestPrebuiltTokenizerDeprecation("edgeNGram", "edge_ngram", |
| 208 | + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false); |
| 209 | + doTestPrebuiltTokenizerDeprecation("nGram", "ngram", Version.V_7_6_0, true); |
| 210 | + doTestPrebuiltTokenizerDeprecation("edgeNGram", "edge_ngram", Version.V_7_6_0, true); |
| 211 | + |
| 212 | + // same batch of tests for custom tokenizer definition in the settings |
| 213 | + doTestCustomTokenizerDeprecation("nGram", "ngram", |
| 214 | + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false); |
| 215 | + doTestCustomTokenizerDeprecation("edgeNGram", "edge_ngram", |
| 216 | + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false); |
| 217 | + doTestCustomTokenizerDeprecation("nGram", "ngram", Version.V_7_6_0, true); |
| 218 | + doTestCustomTokenizerDeprecation("edgeNGram", "edge_ngram", Version.V_7_6_0, true); |
| 219 | + } |
| 220 | + |
| 221 | + public void doTestPrebuiltTokenizerDeprecation(String deprecatedName, String replacement, Version version, boolean expectWarning) |
| 222 | + throws IOException { |
| 223 | + final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) |
| 224 | + .put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); |
| 225 | + |
| 226 | + try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { |
| 227 | + Map<String, TokenizerFactory> tokenizers = createTestAnalysis( |
| 228 | + IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin).tokenizer; |
| 229 | + TokenizerFactory tokenizerFactory = tokenizers.get(deprecatedName); |
| 230 | + |
| 231 | + Tokenizer tokenizer = tokenizerFactory.create(); |
| 232 | + assertNotNull(tokenizer); |
| 233 | + if (expectWarning) { |
| 234 | + assertWarnings("The [" + deprecatedName + "] tokenizer name is deprecated and will be removed in a future version. " |
| 235 | + + "Please change the tokenizer name to [" + replacement + "] instead."); |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + public void doTestCustomTokenizerDeprecation(String deprecatedName, String replacement, Version version, boolean expectWarning) |
| 241 | + throws IOException { |
| 242 | + final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) |
| 243 | + .put(IndexMetaData.SETTING_VERSION_CREATED, version) |
| 244 | + .put("index.analysis.analyzer.custom_analyzer.type", "custom") |
| 245 | + .put("index.analysis.analyzer.custom_analyzer.tokenizer", "my_tokenizer") |
| 246 | + .put("index.analysis.tokenizer.my_tokenizer.type", deprecatedName) |
| 247 | + .build(); |
| 248 | + |
| 249 | + try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { |
| 250 | + createTestAnalysis(IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin); |
| 251 | + |
| 252 | + if (expectWarning) { |
| 253 | + assertWarnings("The [" + deprecatedName + "] tokenizer name is deprecated and will be removed in a future version. " |
| 254 | + + "Please change the tokenizer name to [" + replacement + "] instead."); |
| 255 | + } |
| 256 | + } |
| 257 | + } |
195 | 258 | }
|
0 commit comments