|
26 | 26 | import org.elasticsearch.common.settings.Settings;
|
27 | 27 | import org.elasticsearch.env.Environment;
|
28 | 28 | import org.elasticsearch.index.analysis.TokenFilterFactory;
|
| 29 | +import org.elasticsearch.index.analysis.TokenizerFactory; |
29 | 30 | import org.elasticsearch.test.ESTestCase;
|
30 | 31 | import org.elasticsearch.test.IndexSettingsModule;
|
31 | 32 | import org.elasticsearch.test.VersionUtils;
|
@@ -126,4 +127,87 @@ public void testEdgeNGramFilterInCustomAnalyzerDeprecationError() throws IOExcep
|
126 | 127 | + "Please change the filter name to [edge_ngram] instead.");
|
127 | 128 | }
|
128 | 129 | }
|
| 130 | + |
| 131 | + /** |
| 132 | + * Check that we log a deprecation warning for "nGram" and "edgeNGram" tokenizer names with 7.6 and |
| 133 | + * disallow usages for indices created after 8.0 |
| 134 | + */ |
| 135 | + public void testNGramTokenizerDeprecation() throws IOException { |
| 136 | + // tests for prebuilt tokenizer |
| 137 | + doTestPrebuiltTokenizerDeprecation("nGram", "ngram", |
| 138 | + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false); |
| 139 | + doTestPrebuiltTokenizerDeprecation("edgeNGram", "edge_ngram", |
| 140 | + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false); |
| 141 | + doTestPrebuiltTokenizerDeprecation("nGram", "ngram", |
| 142 | + VersionUtils.randomVersionBetween(random(), Version.V_7_6_0, |
| 143 | + Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0))), |
| 144 | + true); |
| 145 | + doTestPrebuiltTokenizerDeprecation("edgeNGram", "edge_ngram", |
| 146 | + VersionUtils.randomVersionBetween(random(), Version.V_7_6_0, |
| 147 | + Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0))), true); |
| 148 | + expectThrows(IllegalArgumentException.class, () -> doTestPrebuiltTokenizerDeprecation("nGram", "ngram", |
| 149 | + VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), true)); |
| 150 | + expectThrows(IllegalArgumentException.class, () -> doTestPrebuiltTokenizerDeprecation("edgeNGram", "edge_ngram", |
| 151 | + VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), true)); |
| 152 | + |
| 153 | + // same batch of tests for custom tokenizer definition in the settings |
| 154 | + doTestCustomTokenizerDeprecation("nGram", "ngram", |
| 155 | + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false); |
| 156 | + doTestCustomTokenizerDeprecation("edgeNGram", "edge_ngram", |
| 157 | + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false); |
| 158 | + doTestCustomTokenizerDeprecation("nGram", "ngram", |
| 159 | + VersionUtils.randomVersionBetween(random(), Version.V_7_6_0, |
| 160 | + Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0))), |
| 161 | + true); |
| 162 | + doTestCustomTokenizerDeprecation("edgeNGram", "edge_ngram", |
| 163 | + VersionUtils.randomVersionBetween(random(), Version.V_7_6_0, |
| 164 | + Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0))), true); |
| 165 | + expectThrows(IllegalArgumentException.class, () -> doTestCustomTokenizerDeprecation("nGram", "ngram", |
| 166 | + VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), true)); |
| 167 | + expectThrows(IllegalArgumentException.class, () -> doTestCustomTokenizerDeprecation("edgeNGram", "edge_ngram", |
| 168 | + VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), true)); |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + public void doTestPrebuiltTokenizerDeprecation(String deprecatedName, String replacement, Version version, boolean expectWarning) |
| 173 | + throws IOException { |
| 174 | + final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) |
| 175 | + .put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); |
| 176 | + |
| 177 | + try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { |
| 178 | + Map<String, TokenizerFactory> tokenizers = createTestAnalysis( |
| 179 | + IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin).tokenizer; |
| 180 | + TokenizerFactory tokenizerFactory = tokenizers.get(deprecatedName); |
| 181 | + |
| 182 | + Tokenizer tokenizer = tokenizerFactory.create(); |
| 183 | + assertNotNull(tokenizer); |
| 184 | + if (expectWarning) { |
| 185 | + assertWarnings("The [" + deprecatedName + "] tokenizer name is deprecated and will be removed in a future version. " |
| 186 | + + "Please change the tokenizer name to [" + replacement + "] instead."); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + public void doTestCustomTokenizerDeprecation(String deprecatedName, String replacement, Version version, boolean expectWarning) |
| 192 | + throws IOException { |
| 193 | + final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) |
| 194 | + .put(IndexMetaData.SETTING_VERSION_CREATED, version) |
| 195 | + .put("index.analysis.analyzer.custom_analyzer.type", "custom") |
| 196 | + .put("index.analysis.analyzer.custom_analyzer.tokenizer", "my_tokenizer") |
| 197 | + .put("index.analysis.tokenizer.my_tokenizer.type", deprecatedName) |
| 198 | + .build(); |
| 199 | + |
| 200 | + try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { |
| 201 | + Map<String, TokenizerFactory> tokenizers = createTestAnalysis( |
| 202 | + IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin).tokenizer; |
| 203 | + TokenizerFactory tokenizerFactory = tokenizers.get(deprecatedName); |
| 204 | + |
| 205 | + Tokenizer tokenizer = tokenizerFactory.create(); |
| 206 | + assertNotNull(tokenizer); |
| 207 | + if (expectWarning) { |
| 208 | + assertWarnings("The [" + deprecatedName + "] tokenizer name is deprecated and will be removed in a future version. " |
| 209 | + + "Please change the tokenizer name to [" + replacement + "] instead."); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
129 | 213 | }
|
0 commit comments