Backport of lowercase normalizer PR #53882
A pre-configured normalizer for lower-casing. Closes #53872
This commit is contained in:
parent
9f22c0d37c
commit
2da2305587
|
@ -13,11 +13,13 @@ following: `arabic_normalization`, `asciifolding`, `bengali_normalization`,
|
||||||
`persian_normalization`, `scandinavian_folding`, `serbian_normalization`,
|
`persian_normalization`, `scandinavian_folding`, `serbian_normalization`,
|
||||||
`sorani_normalization`, `uppercase`.
|
`sorani_normalization`, `uppercase`.
|
||||||
|
|
||||||
|
Elasticsearch ships with a `lowercase` built-in normalizer. For other forms of
|
||||||
|
normalization a custom configuration is required.
|
||||||
|
|
||||||
[float]
|
[float]
|
||||||
=== Custom normalizers
|
=== Custom normalizers
|
||||||
|
|
||||||
Elasticsearch does not ship with built-in normalizers so far, so the only way
|
Custom normalizers take a list of
|
||||||
to get one is by building a custom one. Custom normalizers take a list of char
|
|
||||||
<<analysis-charfilters, character filters>> and a list of
|
<<analysis-charfilters, character filters>> and a list of
|
||||||
<<analysis-tokenfilters,token filters>>.
|
<<analysis-tokenfilters,token filters>>.
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,10 @@ search-time when the `keyword` field is searched via a query parser such as
|
||||||
the <<query-dsl-match-query,`match`>> query or via a term-level query
|
the <<query-dsl-match-query,`match`>> query or via a term-level query
|
||||||
such as the <<query-dsl-term-query,`term`>> query.
|
such as the <<query-dsl-term-query,`term`>> query.
|
||||||
|
|
||||||
|
A simple normalizer called `lowercase` ships with elasticsearch and can be used.
|
||||||
|
Custom normalizers can be defined as part of analysis settings as follows.
|
||||||
|
|
||||||
|
|
||||||
[source,console]
|
[source,console]
|
||||||
--------------------------------
|
--------------------------------
|
||||||
PUT index
|
PUT index
|
||||||
|
|
|
@ -299,7 +299,6 @@ public final class AnalysisRegistry implements Closeable {
|
||||||
|
|
||||||
private Map<String, AnalyzerProvider<?>> buildNormalizerFactories(IndexSettings indexSettings) throws IOException {
|
private Map<String, AnalyzerProvider<?>> buildNormalizerFactories(IndexSettings indexSettings) throws IOException {
|
||||||
final Map<String, Settings> normalizersSettings = indexSettings.getSettings().getGroups("index.analysis.normalizer");
|
final Map<String, Settings> normalizersSettings = indexSettings.getSettings().getGroups("index.analysis.normalizer");
|
||||||
// TODO: Have pre-built normalizers
|
|
||||||
return buildMapping(Component.NORMALIZER, indexSettings, normalizersSettings, normalizers, Collections.emptyMap());
|
return buildMapping(Component.NORMALIZER, indexSettings, normalizersSettings, normalizers, Collections.emptyMap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* Licensed to Elasticsearch under one or more contributor
|
||||||
|
* license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright
|
||||||
|
* ownership. Elasticsearch licenses this file to you under
|
||||||
|
* the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
* not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.elasticsearch.index.analysis;
|
||||||
|
|
||||||
|
import org.apache.lucene.analysis.Analyzer;
|
||||||
|
import org.apache.lucene.analysis.LowerCaseFilter;
|
||||||
|
import org.apache.lucene.analysis.TokenStream;
|
||||||
|
import org.apache.lucene.analysis.Tokenizer;
|
||||||
|
import org.apache.lucene.analysis.core.KeywordTokenizer;
|
||||||
|
|
||||||
|
/** Normalizer used to lowercase values */
|
||||||
|
public final class LowercaseNormalizer extends Analyzer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected TokenStreamComponents createComponents(String s) {
|
||||||
|
final Tokenizer tokenizer = new KeywordTokenizer();
|
||||||
|
TokenStream stream = new LowerCaseFilter(tokenizer);
|
||||||
|
return new TokenStreamComponents(tokenizer, stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected TokenStream normalize(String fieldName, TokenStream in) {
|
||||||
|
return new LowerCaseFilter(in);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* Licensed to Elasticsearch under one or more contributor
|
||||||
|
* license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright
|
||||||
|
* ownership. Elasticsearch licenses this file to you under
|
||||||
|
* the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
* not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package org.elasticsearch.index.analysis;
|
||||||
|
|
||||||
|
import org.elasticsearch.common.settings.Settings;
|
||||||
|
import org.elasticsearch.env.Environment;
|
||||||
|
import org.elasticsearch.index.IndexSettings;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds an analyzer for normalization that lowercases terms.
|
||||||
|
*/
|
||||||
|
public class LowercaseNormalizerProvider extends AbstractIndexAnalyzerProvider<LowercaseNormalizer> {
|
||||||
|
|
||||||
|
private final LowercaseNormalizer analyzer;
|
||||||
|
|
||||||
|
public LowercaseNormalizerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) {
|
||||||
|
super(indexSettings, name, settings);
|
||||||
|
this.analyzer = new LowercaseNormalizer();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LowercaseNormalizer get() {
|
||||||
|
return analyzer;
|
||||||
|
}
|
||||||
|
}
|
|
@ -35,6 +35,7 @@ import org.elasticsearch.index.analysis.AnalyzerProvider;
|
||||||
import org.elasticsearch.index.analysis.CharFilterFactory;
|
import org.elasticsearch.index.analysis.CharFilterFactory;
|
||||||
import org.elasticsearch.index.analysis.HunspellTokenFilterFactory;
|
import org.elasticsearch.index.analysis.HunspellTokenFilterFactory;
|
||||||
import org.elasticsearch.index.analysis.KeywordAnalyzerProvider;
|
import org.elasticsearch.index.analysis.KeywordAnalyzerProvider;
|
||||||
|
import org.elasticsearch.index.analysis.LowercaseNormalizerProvider;
|
||||||
import org.elasticsearch.index.analysis.PreBuiltAnalyzerProviderFactory;
|
import org.elasticsearch.index.analysis.PreBuiltAnalyzerProviderFactory;
|
||||||
import org.elasticsearch.index.analysis.PreConfiguredCharFilter;
|
import org.elasticsearch.index.analysis.PreConfiguredCharFilter;
|
||||||
import org.elasticsearch.index.analysis.PreConfiguredTokenFilter;
|
import org.elasticsearch.index.analysis.PreConfiguredTokenFilter;
|
||||||
|
@ -250,7 +251,7 @@ public final class AnalysisModule {
|
||||||
|
|
||||||
private NamedRegistry<AnalysisProvider<AnalyzerProvider<?>>> setupNormalizers(List<AnalysisPlugin> plugins) {
|
private NamedRegistry<AnalysisProvider<AnalyzerProvider<?>>> setupNormalizers(List<AnalysisPlugin> plugins) {
|
||||||
NamedRegistry<AnalysisProvider<AnalyzerProvider<?>>> normalizers = new NamedRegistry<>("normalizer");
|
NamedRegistry<AnalysisProvider<AnalyzerProvider<?>>> normalizers = new NamedRegistry<>("normalizer");
|
||||||
// TODO: provide built-in normalizer providers?
|
normalizers.register("lowercase", LowercaseNormalizerProvider::new);
|
||||||
// TODO: pluggability?
|
// TODO: pluggability?
|
||||||
return normalizers;
|
return normalizers;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,11 +20,13 @@
|
||||||
package org.elasticsearch.index.analysis;
|
package org.elasticsearch.index.analysis;
|
||||||
|
|
||||||
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
|
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
|
||||||
|
|
||||||
import org.apache.lucene.analysis.Analyzer;
|
import org.apache.lucene.analysis.Analyzer;
|
||||||
import org.apache.lucene.analysis.MockTokenFilter;
|
import org.apache.lucene.analysis.MockTokenFilter;
|
||||||
import org.apache.lucene.analysis.TokenStream;
|
import org.apache.lucene.analysis.TokenStream;
|
||||||
import org.apache.lucene.analysis.Tokenizer;
|
import org.apache.lucene.analysis.Tokenizer;
|
||||||
import org.apache.lucene.analysis.en.EnglishAnalyzer;
|
import org.apache.lucene.analysis.en.EnglishAnalyzer;
|
||||||
|
import org.apache.lucene.analysis.reverse.ReverseStringFilter;
|
||||||
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||||
import org.apache.lucene.analysis.standard.StandardTokenizer;
|
import org.apache.lucene.analysis.standard.StandardTokenizer;
|
||||||
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
||||||
|
@ -39,6 +41,7 @@ import org.elasticsearch.indices.analysis.AnalysisModule;
|
||||||
import org.elasticsearch.indices.analysis.AnalysisModule.AnalysisProvider;
|
import org.elasticsearch.indices.analysis.AnalysisModule.AnalysisProvider;
|
||||||
import org.elasticsearch.indices.analysis.PreBuiltAnalyzers;
|
import org.elasticsearch.indices.analysis.PreBuiltAnalyzers;
|
||||||
import org.elasticsearch.plugins.AnalysisPlugin;
|
import org.elasticsearch.plugins.AnalysisPlugin;
|
||||||
|
import org.elasticsearch.plugins.Plugin;
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
import org.elasticsearch.test.IndexSettingsModule;
|
import org.elasticsearch.test.IndexSettingsModule;
|
||||||
import org.elasticsearch.test.VersionUtils;
|
import org.elasticsearch.test.VersionUtils;
|
||||||
|
@ -46,6 +49,7 @@ import org.elasticsearch.test.VersionUtils;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static java.util.Collections.emptyMap;
|
import static java.util.Collections.emptyMap;
|
||||||
|
@ -58,6 +62,7 @@ import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
public class AnalysisRegistryTests extends ESTestCase {
|
public class AnalysisRegistryTests extends ESTestCase {
|
||||||
private AnalysisRegistry emptyRegistry;
|
private AnalysisRegistry emptyRegistry;
|
||||||
|
private AnalysisRegistry nonEmptyRegistry;
|
||||||
|
|
||||||
private static AnalyzerProvider<?> analyzerProvider(final String name) {
|
private static AnalyzerProvider<?> analyzerProvider(final String name) {
|
||||||
return new PreBuiltAnalyzerProvider(name, AnalyzerScope.INDEX, new EnglishAnalyzer());
|
return new PreBuiltAnalyzerProvider(name, AnalyzerScope.INDEX, new EnglishAnalyzer());
|
||||||
|
@ -68,6 +73,16 @@ public class AnalysisRegistryTests extends ESTestCase {
|
||||||
emptyMap(), emptyMap(), emptyMap(), emptyMap());
|
emptyMap(), emptyMap(), emptyMap(), emptyMap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a reverse filter available for use in testNameClashNormalizer test
|
||||||
|
*/
|
||||||
|
public static class MockAnalysisPlugin extends Plugin implements AnalysisPlugin {
|
||||||
|
@Override
|
||||||
|
public List<PreConfiguredTokenFilter> getPreConfiguredTokenFilters() {
|
||||||
|
return singletonList(PreConfiguredTokenFilter.singleton("reverse", true, ReverseStringFilter::new));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static IndexSettings indexSettingsOfCurrentVersion(Settings.Builder settings) {
|
private static IndexSettings indexSettingsOfCurrentVersion(Settings.Builder settings) {
|
||||||
return IndexSettingsModule.newIndexSettings("index", settings
|
return IndexSettingsModule.newIndexSettings("index", settings
|
||||||
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
|
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
|
||||||
|
@ -77,9 +92,13 @@ public class AnalysisRegistryTests extends ESTestCase {
|
||||||
@Override
|
@Override
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
emptyRegistry = emptyAnalysisRegistry(Settings.builder()
|
Settings settings = Settings.builder()
|
||||||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
|
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
|
||||||
.build());
|
.build();
|
||||||
|
emptyRegistry = emptyAnalysisRegistry(settings);
|
||||||
|
// Module loaded to register in-built normalizers for testing
|
||||||
|
AnalysisModule module = new AnalysisModule(TestEnvironment.newEnvironment(settings), singletonList(new MockAnalysisPlugin()));
|
||||||
|
nonEmptyRegistry = module.getAnalysisRegistry();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDefaultAnalyzers() throws IOException {
|
public void testDefaultAnalyzers() throws IOException {
|
||||||
|
@ -136,6 +155,28 @@ public class AnalysisRegistryTests extends ESTestCase {
|
||||||
assertEquals("analyzer [default] contains filters [my_filter] that are not allowed to run in all mode.", ex.getMessage());
|
assertEquals("analyzer [default] contains filters [my_filter] that are not allowed to run in all mode.", ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void testNameClashNormalizer() throws IOException {
|
||||||
|
|
||||||
|
// Test out-of-the-box normalizer works OK.
|
||||||
|
IndexAnalyzers indexAnalyzers = nonEmptyRegistry.build(IndexSettingsModule.newIndexSettings("index", Settings.EMPTY));
|
||||||
|
assertNotNull(indexAnalyzers.getNormalizer("lowercase"));
|
||||||
|
assertThat(indexAnalyzers.getNormalizer("lowercase").normalize("field", "AbC").utf8ToString(), equalTo("abc"));
|
||||||
|
|
||||||
|
// Test that a name clash with a custom normalizer will favour the index's normalizer rather than the out-of-the-box
|
||||||
|
// one of the same name. (However this "feature" will be removed with https://github.com/elastic/elasticsearch/issues/22263 )
|
||||||
|
Settings settings = Settings.builder()
|
||||||
|
// Deliberately bad choice of normalizer name for the job it does.
|
||||||
|
.put("index.analysis.normalizer.lowercase.type", "custom")
|
||||||
|
.putList("index.analysis.normalizer.lowercase.filter", "reverse")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
indexAnalyzers = nonEmptyRegistry.build(IndexSettingsModule.newIndexSettings("index", settings));
|
||||||
|
assertNotNull(indexAnalyzers.getNormalizer("lowercase"));
|
||||||
|
assertThat(indexAnalyzers.getNormalizer("lowercase").normalize("field","AbC").utf8ToString(), equalTo("CbA"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testOverrideDefaultIndexAnalyzerIsUnsupported() {
|
public void testOverrideDefaultIndexAnalyzerIsUnsupported() {
|
||||||
Version version = VersionUtils.randomVersionBetween(random(), Version.V_6_0_0_alpha1, Version.CURRENT);
|
Version version = VersionUtils.randomVersionBetween(random(), Version.V_6_0_0_alpha1, Version.CURRENT);
|
||||||
Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build();
|
Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build();
|
||||||
|
|
|
@ -344,10 +344,18 @@ public class KeywordFieldMapperTests extends ESSingleNodeTestCase {
|
||||||
assertEquals(0, fieldNamesFields.length);
|
assertEquals(0, fieldNamesFields.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNormalizer() throws IOException {
|
public void testCustomNormalizer() throws IOException {
|
||||||
|
checkLowercaseNormalizer("my_lowercase");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testInBuiltNormalizer() throws IOException {
|
||||||
|
checkLowercaseNormalizer("lowercase");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void checkLowercaseNormalizer(String normalizerName) throws IOException {
|
||||||
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
|
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||||
.startObject("properties").startObject("field")
|
.startObject("properties").startObject("field")
|
||||||
.field("type", "keyword").field("normalizer", "my_lowercase").endObject().endObject()
|
.field("type", "keyword").field("normalizer", normalizerName).endObject().endObject()
|
||||||
.endObject().endObject());
|
.endObject().endObject());
|
||||||
|
|
||||||
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
|
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
|
||||||
|
|
Loading…
Reference in New Issue