|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.analysis.common; |
| 21 | + |
| 22 | +import org.apache.lucene.analysis.MockTokenizer; |
| 23 | +import org.apache.lucene.analysis.Tokenizer; |
| 24 | +import org.elasticsearch.Version; |
| 25 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 26 | +import org.elasticsearch.common.settings.Settings; |
| 27 | +import org.elasticsearch.env.Environment; |
| 28 | +import org.elasticsearch.index.IndexSettings; |
| 29 | +import org.elasticsearch.index.analysis.TokenFilterFactory; |
| 30 | +import org.elasticsearch.test.ESTestCase; |
| 31 | +import org.elasticsearch.test.IndexSettingsModule; |
| 32 | +import org.elasticsearch.test.VersionUtils; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.StringReader; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +public class CommonAnalysisPluginTests extends ESTestCase { |
| 39 | + |
| 40 | + /** |
| 41 | + * Check that the deprecated name "nGram" issues a deprecation warning for indices created since 6.3.0 |
| 42 | + */ |
| 43 | + public void testNGramDeprecationWarning() throws IOException { |
| 44 | + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) |
| 45 | + .put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_6_4_0, Version.CURRENT)) |
| 46 | + .build(); |
| 47 | + |
| 48 | + IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); |
| 49 | + try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { |
| 50 | + Map<String, TokenFilterFactory> tokenFilters = createTestAnalysis(idxSettings, settings, commonAnalysisPlugin).tokenFilter; |
| 51 | + TokenFilterFactory tokenFilterFactory = tokenFilters.get("nGram"); |
| 52 | + Tokenizer tokenizer = new MockTokenizer(); |
| 53 | + tokenizer.setReader(new StringReader("foo bar")); |
| 54 | + assertNotNull(tokenFilterFactory.create(tokenizer)); |
| 55 | + assertWarnings( |
| 56 | + "The [nGram] token filter name is deprecated and will be removed in a future version. " |
| 57 | + + "Please change the filter name to [ngram] instead."); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Check that the deprecated name "nGram" does NOT issues a deprecation warning for indices created before 6.4.0 |
| 63 | + */ |
| 64 | + public void testNGramNoDeprecationWarningPre6_4() throws IOException { |
| 65 | + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) |
| 66 | + .put(IndexMetaData.SETTING_VERSION_CREATED, |
| 67 | + VersionUtils.randomVersionBetween(random(), Version.V_5_0_0, Version.V_6_3_0)) |
| 68 | + .build(); |
| 69 | + |
| 70 | + IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); |
| 71 | + try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { |
| 72 | + Map<String, TokenFilterFactory> tokenFilters = createTestAnalysis(idxSettings, settings, commonAnalysisPlugin).tokenFilter; |
| 73 | + TokenFilterFactory tokenFilterFactory = tokenFilters.get("nGram"); |
| 74 | + Tokenizer tokenizer = new MockTokenizer(); |
| 75 | + tokenizer.setReader(new StringReader("foo bar")); |
| 76 | + assertNotNull(tokenFilterFactory.create(tokenizer)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Check that the deprecated name "edgeNGram" issues a deprecation warning for indices created since 6.3.0 |
| 82 | + */ |
| 83 | + public void testEdgeNGramDeprecationWarning() throws IOException { |
| 84 | + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) |
| 85 | + .put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_6_4_0, Version.CURRENT)) |
| 86 | + .build(); |
| 87 | + |
| 88 | + IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); |
| 89 | + try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { |
| 90 | + Map<String, TokenFilterFactory> tokenFilters = createTestAnalysis(idxSettings, settings, commonAnalysisPlugin).tokenFilter; |
| 91 | + TokenFilterFactory tokenFilterFactory = tokenFilters.get("edgeNGram"); |
| 92 | + Tokenizer tokenizer = new MockTokenizer(); |
| 93 | + tokenizer.setReader(new StringReader("foo bar")); |
| 94 | + assertNotNull(tokenFilterFactory.create(tokenizer)); |
| 95 | + assertWarnings( |
| 96 | + "The [edgeNGram] token filter name is deprecated and will be removed in a future version. " |
| 97 | + + "Please change the filter name to [edge_ngram] instead."); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Check that the deprecated name "edgeNGram" does NOT issues a deprecation warning for indices created before 6.4.0 |
| 103 | + */ |
| 104 | + public void testEdgeNGramNoDeprecationWarningPre6_4() throws IOException { |
| 105 | + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) |
| 106 | + .put(IndexMetaData.SETTING_VERSION_CREATED, |
| 107 | + VersionUtils.randomVersionBetween(random(), Version.V_5_0_0, Version.V_6_3_0)) |
| 108 | + .build(); |
| 109 | + |
| 110 | + IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); |
| 111 | + try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { |
| 112 | + Map<String, TokenFilterFactory> tokenFilters = createTestAnalysis(idxSettings, settings, commonAnalysisPlugin).tokenFilter; |
| 113 | + TokenFilterFactory tokenFilterFactory = tokenFilters.get("edgeNGram"); |
| 114 | + Tokenizer tokenizer = new MockTokenizer(); |
| 115 | + tokenizer.setReader(new StringReader("foo bar")); |
| 116 | + assertNotNull(tokenFilterFactory.create(tokenizer)); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments