Skip to content

Commit 0b7eb2a

Browse files
author
Christoph Büscher
committed
Log deprecation for nGram and edgeNGram custom filters (elastic#50376)
The camel-case `nGram` and `edgeNGram` filter names were deprecated in 6. We currently throw errors on new indices when they are used. However these errors are currently only thrown for pre-configured filters, adding them as custom filters doesn't trigger the warning and error. This change adds the appropriate deprecation warnings for `nGram` and `edgeNGram` respectively on version 7 indices. Relates elastic#50360
1 parent c37c53a commit 0b7eb2a

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonAnalysisPlugin.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@
118118
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
119119
import org.elasticsearch.common.logging.DeprecationLogger;
120120
import org.elasticsearch.common.regex.Regex;
121+
import org.elasticsearch.common.settings.Settings;
121122
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
122123
import org.elasticsearch.env.Environment;
123124
import org.elasticsearch.env.NodeEnvironment;
125+
import org.elasticsearch.index.IndexSettings;
124126
import org.elasticsearch.index.analysis.AnalyzerProvider;
125127
import org.elasticsearch.index.analysis.CharFilterFactory;
126128
import org.elasticsearch.index.analysis.PreBuiltAnalyzerProviderFactory;
@@ -242,7 +244,12 @@ public Map<String, AnalysisProvider<TokenFilterFactory>> getTokenFilters() {
242244
filters.put("dictionary_decompounder", requiresAnalysisSettings(DictionaryCompoundWordTokenFilterFactory::new));
243245
filters.put("dutch_stem", DutchStemTokenFilterFactory::new);
244246
filters.put("edge_ngram", EdgeNGramTokenFilterFactory::new);
245-
filters.put("edgeNGram", EdgeNGramTokenFilterFactory::new);
247+
filters.put("edgeNGram", (IndexSettings indexSettings, Environment environment, String name, Settings settings) -> {
248+
deprecationLogger.deprecatedAndMaybeLog("edgeNGram_deprecation",
249+
"The [edgeNGram] token filter name is deprecated and will be removed in a future version. "
250+
+ "Please change the filter name to [edge_ngram] instead.");
251+
return new EdgeNGramTokenFilterFactory(indexSettings, environment, name, settings);
252+
});
246253
filters.put("elision", requiresAnalysisSettings(ElisionTokenFilterFactory::new));
247254
filters.put("fingerprint", FingerprintTokenFilterFactory::new);
248255
filters.put("flatten_graph", FlattenGraphTokenFilterFactory::new);
@@ -262,7 +269,12 @@ public Map<String, AnalysisProvider<TokenFilterFactory>> getTokenFilters() {
262269
filters.put("min_hash", MinHashTokenFilterFactory::new);
263270
filters.put("multiplexer", MultiplexerTokenFilterFactory::new);
264271
filters.put("ngram", NGramTokenFilterFactory::new);
265-
filters.put("nGram", NGramTokenFilterFactory::new);
272+
filters.put("nGram", (IndexSettings indexSettings, Environment environment, String name, Settings settings) -> {
273+
deprecationLogger.deprecatedAndMaybeLog("nGram_deprecation",
274+
"The [nGram] token filter name is deprecated and will be removed in a future version. "
275+
+ "Please change the filter name to [ngram] instead.");
276+
return new NGramTokenFilterFactory(indexSettings, environment, name, settings);
277+
});
266278
filters.put("pattern_capture", requiresAnalysisSettings(PatternCaptureGroupTokenFilterFactory::new));
267279
filters.put("pattern_replace", requiresAnalysisSettings(PatternReplaceTokenFilterFactory::new));
268280
filters.put("persian_normalization", PersianNormalizationFilterFactory::new);

modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonAnalysisPluginTests.java

+39
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,43 @@ public void testStandardHtmlStripAnalyzerDeprecationWarning() throws IOException
169169
"replace it with a custom analyzer using [standard] tokenizer and [html_strip] char_filter, plus [lowercase] filter");
170170
}
171171
}
172+
173+
/**
174+
* Check that the deprecated "nGram" filter logs a warning when the filter is used as a custom filter
175+
*/
176+
public void testnGramFilterInCustomAnalyzerDeprecationError() throws IOException {
177+
final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
178+
.put(IndexMetaData.SETTING_VERSION_CREATED,
179+
VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.CURRENT))
180+
.put("index.analysis.analyzer.custom_analyzer.type", "custom")
181+
.put("index.analysis.analyzer.custom_analyzer.tokenizer", "standard")
182+
.putList("index.analysis.analyzer.custom_analyzer.filter", "my_ngram")
183+
.put("index.analysis.filter.my_ngram.type", "nGram")
184+
.build();
185+
186+
final CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin();
187+
188+
createTestAnalysis(IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin);
189+
assertWarnings("The [nGram] token filter name is deprecated and will be removed in a future version. "
190+
+ "Please change the filter name to [ngram] instead.");
191+
}
192+
193+
/**
194+
* Check that the deprecated "edgeNGram" filter logs a warning when the filter is used as a custom filter
195+
*/
196+
public void testEdgeNGramFilterInCustomAnalyzerDeprecationError() throws IOException {
197+
final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
198+
.put(IndexMetaData.SETTING_VERSION_CREATED,
199+
VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.CURRENT))
200+
.put("index.analysis.analyzer.custom_analyzer.type", "custom")
201+
.put("index.analysis.analyzer.custom_analyzer.tokenizer", "standard")
202+
.putList("index.analysis.analyzer.custom_analyzer.filter", "my_ngram")
203+
.put("index.analysis.filter.my_ngram.type", "edgeNGram")
204+
.build();
205+
final CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin();
206+
207+
createTestAnalysis(IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin);
208+
assertWarnings("The [edgeNGram] token filter name is deprecated and will be removed in a future version. "
209+
+ "Please change the filter name to [edge_ngram] instead.");
210+
}
172211
}

0 commit comments

Comments
 (0)