|
20 | 20 | package org.elasticsearch.index.analysis;
|
21 | 21 |
|
22 | 22 | import com.carrotsearch.randomizedtesting.generators.RandomPicks;
|
23 |
| - |
24 | 23 | import org.apache.lucene.analysis.Analyzer;
|
25 | 24 | import org.apache.lucene.analysis.MockTokenFilter;
|
26 | 25 | import org.apache.lucene.analysis.TokenStream;
|
| 26 | +import org.apache.lucene.analysis.Tokenizer; |
27 | 27 | import org.apache.lucene.analysis.en.EnglishAnalyzer;
|
28 | 28 | import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
| 29 | +import org.apache.lucene.analysis.standard.StandardTokenizer; |
29 | 30 | import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
30 | 31 | import org.elasticsearch.Version;
|
31 | 32 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
@@ -108,19 +109,25 @@ public void testOverrideDefaultAnalyzer() throws IOException {
|
108 | 109 | public void testOverrideDefaultAnalyzerWithoutAnalysisModeAll() throws IOException {
|
109 | 110 | Version version = VersionUtils.randomVersion(random());
|
110 | 111 | Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
|
111 |
| - TokenFilterFactory tokenFilter = new AbstractTokenFilterFactory(IndexSettingsModule.newIndexSettings("index", settings), |
112 |
| - "my_filter", Settings.EMPTY) { |
| 112 | + IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("index", settings); |
| 113 | + TokenFilterFactory tokenFilter = new AbstractTokenFilterFactory(indexSettings, "my_filter", Settings.EMPTY) { |
113 | 114 | @Override
|
114 | 115 | public AnalysisMode getAnalysisMode() {
|
115 | 116 | return randomFrom(AnalysisMode.SEARCH_TIME, AnalysisMode.INDEX_TIME);
|
116 | 117 | }
|
117 | 118 |
|
118 | 119 | @Override
|
119 | 120 | public TokenStream create(TokenStream tokenStream) {
|
120 |
| - return null; |
| 121 | + return tokenStream; |
121 | 122 | }
|
122 | 123 | };
|
123 |
| - Analyzer analyzer = new CustomAnalyzer(null, new CharFilterFactory[0], new TokenFilterFactory[] { tokenFilter }); |
| 124 | + TokenizerFactory tokenizer = new AbstractTokenizerFactory(indexSettings, Settings.EMPTY, "my_tokenizer") { |
| 125 | + @Override |
| 126 | + public Tokenizer create() { |
| 127 | + return new StandardTokenizer(); |
| 128 | + } |
| 129 | + }; |
| 130 | + Analyzer analyzer = new CustomAnalyzer(tokenizer, new CharFilterFactory[0], new TokenFilterFactory[] { tokenFilter }); |
124 | 131 | MapperException ex = expectThrows(MapperException.class,
|
125 | 132 | () -> emptyRegistry.build(IndexSettingsModule.newIndexSettings("index", settings),
|
126 | 133 | singletonMap("default", new PreBuiltAnalyzerProvider("default", AnalyzerScope.INDEX, analyzer)), emptyMap(),
|
@@ -264,4 +271,122 @@ public void testEnsureCloseInvocationProperlyDelegated() throws IOException {
|
264 | 271 | registry.close();
|
265 | 272 | verify(mock).close();
|
266 | 273 | }
|
| 274 | + |
| 275 | + public void testDeprecationsAndExceptions() throws IOException { |
| 276 | + |
| 277 | + AnalysisPlugin plugin = new AnalysisPlugin() { |
| 278 | + |
| 279 | + class MockFactory extends AbstractTokenFilterFactory { |
| 280 | + MockFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) { |
| 281 | + super(indexSettings, name, settings); |
| 282 | + } |
| 283 | + |
| 284 | + @Override |
| 285 | + public TokenStream create(TokenStream tokenStream) { |
| 286 | + if (indexSettings.getIndexVersionCreated().equals(Version.CURRENT)) { |
| 287 | + deprecationLogger.deprecated("Using deprecated token filter [deprecated]"); |
| 288 | + } |
| 289 | + return tokenStream; |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + class ExceptionFactory extends AbstractTokenFilterFactory { |
| 294 | + |
| 295 | + ExceptionFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) { |
| 296 | + super(indexSettings, name, settings); |
| 297 | + } |
| 298 | + |
| 299 | + @Override |
| 300 | + public TokenStream create(TokenStream tokenStream) { |
| 301 | + if (indexSettings.getIndexVersionCreated().equals(Version.CURRENT)) { |
| 302 | + throw new IllegalArgumentException("Cannot use token filter [exception]"); |
| 303 | + } |
| 304 | + return tokenStream; |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + class UnusedMockFactory extends AbstractTokenFilterFactory { |
| 309 | + UnusedMockFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) { |
| 310 | + super(indexSettings, name, settings); |
| 311 | + } |
| 312 | + |
| 313 | + @Override |
| 314 | + public TokenStream create(TokenStream tokenStream) { |
| 315 | + deprecationLogger.deprecated("Using deprecated token filter [unused]"); |
| 316 | + return tokenStream; |
| 317 | + } |
| 318 | + } |
| 319 | + |
| 320 | + class NormalizerFactory extends AbstractTokenFilterFactory implements NormalizingTokenFilterFactory { |
| 321 | + |
| 322 | + NormalizerFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) { |
| 323 | + super(indexSettings, name, settings); |
| 324 | + } |
| 325 | + |
| 326 | + @Override |
| 327 | + public TokenStream create(TokenStream tokenStream) { |
| 328 | + deprecationLogger.deprecated("Using deprecated token filter [deprecated_normalizer]"); |
| 329 | + return tokenStream; |
| 330 | + } |
| 331 | + |
| 332 | + } |
| 333 | + |
| 334 | + @Override |
| 335 | + public Map<String, AnalysisProvider<TokenFilterFactory>> getTokenFilters() { |
| 336 | + return Map.of("deprecated", MockFactory::new, "unused", UnusedMockFactory::new, |
| 337 | + "deprecated_normalizer", NormalizerFactory::new, "exception", ExceptionFactory::new); |
| 338 | + } |
| 339 | + }; |
| 340 | + |
| 341 | + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); |
| 342 | + Settings indexSettings = Settings.builder() |
| 343 | + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) |
| 344 | + .put("index.analysis.filter.deprecated.type", "deprecated") |
| 345 | + .put("index.analysis.analyzer.custom.tokenizer", "standard") |
| 346 | + .putList("index.analysis.analyzer.custom.filter", "lowercase", "deprecated") |
| 347 | + .build(); |
| 348 | + |
| 349 | + IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", indexSettings); |
| 350 | + |
| 351 | + new AnalysisModule(TestEnvironment.newEnvironment(settings), |
| 352 | + singletonList(plugin)).getAnalysisRegistry().build(idxSettings); |
| 353 | + |
| 354 | + // We should only get a warning from the token filter that is referenced in settings |
| 355 | + assertWarnings("Using deprecated token filter [deprecated]"); |
| 356 | + |
| 357 | + indexSettings = Settings.builder() |
| 358 | + .put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.getPreviousVersion()) |
| 359 | + .put("index.analysis.filter.deprecated.type", "deprecated_normalizer") |
| 360 | + .putList("index.analysis.normalizer.custom.filter", "lowercase", "deprecated_normalizer") |
| 361 | + .put("index.analysis.filter.deprecated.type", "deprecated") |
| 362 | + .put("index.analysis.filter.exception.type", "exception") |
| 363 | + .put("index.analysis.analyzer.custom.tokenizer", "standard") |
| 364 | + // exception will not throw because we're not on Version.CURRENT |
| 365 | + .putList("index.analysis.analyzer.custom.filter", "lowercase", "deprecated", "exception") |
| 366 | + .build(); |
| 367 | + idxSettings = IndexSettingsModule.newIndexSettings("index", indexSettings); |
| 368 | + |
| 369 | + new AnalysisModule(TestEnvironment.newEnvironment(settings), |
| 370 | + singletonList(plugin)).getAnalysisRegistry().build(idxSettings); |
| 371 | + |
| 372 | + // We should only get a warning from the normalizer, because we're on a version where 'deprecated' |
| 373 | + // works fine |
| 374 | + assertWarnings("Using deprecated token filter [deprecated_normalizer]"); |
| 375 | + |
| 376 | + indexSettings = Settings.builder() |
| 377 | + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) |
| 378 | + .put("index.analysis.filter.exception.type", "exception") |
| 379 | + .put("index.analysis.analyzer.custom.tokenizer", "standard") |
| 380 | + // exception will not throw because we're not on Version.LATEST |
| 381 | + .putList("index.analysis.analyzer.custom.filter", "lowercase", "exception") |
| 382 | + .build(); |
| 383 | + IndexSettings exceptionSettings = IndexSettingsModule.newIndexSettings("index", indexSettings); |
| 384 | + |
| 385 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { |
| 386 | + new AnalysisModule(TestEnvironment.newEnvironment(settings), |
| 387 | + singletonList(plugin)).getAnalysisRegistry().build(exceptionSettings); |
| 388 | + }); |
| 389 | + assertEquals("Cannot use token filter [exception]", e.getMessage()); |
| 390 | + |
| 391 | + } |
267 | 392 | }
|
0 commit comments