Small internal AnalysisRegistry changes (#42500)

Some internal refactorings to the AnalysisRegistry, spin-off from #40782.
This commit is contained in:
Christoph Büscher 2019-05-24 15:27:10 +02:00
parent a5b6ed8d1e
commit 12d5642e93
1 changed files with 33 additions and 46 deletions

View File

@ -26,7 +26,6 @@ import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.env.Environment; import org.elasticsearch.env.Environment;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.mapper.TextFieldMapper; import org.elasticsearch.index.mapper.TextFieldMapper;
import org.elasticsearch.indices.analysis.AnalysisModule; import org.elasticsearch.indices.analysis.AnalysisModule;
@ -40,6 +39,7 @@ import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.Collections.unmodifiableMap; import static java.util.Collections.unmodifiableMap;
@ -159,19 +159,18 @@ public final class AnalysisRegistry implements Closeable {
* Creates an index-level {@link IndexAnalyzers} from this registry using the given index settings * Creates an index-level {@link IndexAnalyzers} from this registry using the given index settings
*/ */
public IndexAnalyzers build(IndexSettings indexSettings) throws IOException { public IndexAnalyzers build(IndexSettings indexSettings) throws IOException {
final Map<String, CharFilterFactory> charFilterFactories = buildCharFilterFactories(indexSettings); final Map<String, CharFilterFactory> charFilterFactories = buildCharFilterFactories(indexSettings);
final Map<String, TokenizerFactory> tokenizerFactories = buildTokenizerFactories(indexSettings); final Map<String, TokenizerFactory> tokenizerFactories = buildTokenizerFactories(indexSettings);
final Map<String, TokenFilterFactory> tokenFilterFactories = buildTokenFilterFactories(indexSettings); final Map<String, TokenFilterFactory> tokenFilterFactories = buildTokenFilterFactories(indexSettings);
final Map<String, AnalyzerProvider<?>> analyzierFactories = buildAnalyzerFactories(indexSettings); final Map<String, AnalyzerProvider<?>> analyzerFactories = buildAnalyzerFactories(indexSettings);
final Map<String, AnalyzerProvider<?>> normalizerFactories = buildNormalizerFactories(indexSettings); final Map<String, AnalyzerProvider<?>> normalizerFactories = buildNormalizerFactories(indexSettings);
return build(indexSettings, analyzierFactories, normalizerFactories, tokenizerFactories, charFilterFactories, tokenFilterFactories); return build(indexSettings, analyzerFactories, normalizerFactories, tokenizerFactories, charFilterFactories, tokenFilterFactories);
} }
public Map<String, TokenFilterFactory> buildTokenFilterFactories(IndexSettings indexSettings) throws IOException { public Map<String, TokenFilterFactory> buildTokenFilterFactories(IndexSettings indexSettings) throws IOException {
final Map<String, Settings> tokenFiltersSettings = indexSettings.getSettings().getGroups(INDEX_ANALYSIS_FILTER); final Map<String, Settings> tokenFiltersSettings = indexSettings.getSettings().getGroups(INDEX_ANALYSIS_FILTER);
return buildMapping(Component.FILTER, indexSettings, tokenFiltersSettings, return buildMapping(Component.FILTER, indexSettings, tokenFiltersSettings, this.tokenFilters,
Collections.unmodifiableMap(this.tokenFilters), prebuiltAnalysis.preConfiguredTokenFilters); prebuiltAnalysis.preConfiguredTokenFilters);
} }
public Map<String, TokenizerFactory> buildTokenizerFactories(IndexSettings indexSettings) throws IOException { public Map<String, TokenizerFactory> buildTokenizerFactories(IndexSettings indexSettings) throws IOException {
@ -205,13 +204,8 @@ public final class AnalysisRegistry implements Closeable {
* @return {@link TokenizerFactory} provider or <code>null</code> * @return {@link TokenizerFactory} provider or <code>null</code>
*/ */
public AnalysisProvider<TokenizerFactory> getTokenizerProvider(String tokenizer, IndexSettings indexSettings) { public AnalysisProvider<TokenizerFactory> getTokenizerProvider(String tokenizer, IndexSettings indexSettings) {
final Map<String, Settings> tokenizerSettings = indexSettings.getSettings().getGroups("index.analysis.tokenizer"); return getProvider(Component.TOKENIZER, tokenizer, indexSettings, "index.analysis.tokenizer", tokenizers,
if (tokenizerSettings.containsKey(tokenizer)) { this::getTokenizerProvider);
Settings currentSettings = tokenizerSettings.get(tokenizer);
return getAnalysisProvider(Component.TOKENIZER, tokenizers, tokenizer, currentSettings.get("type"));
} else {
return getTokenizerProvider(tokenizer);
}
} }
/** /**
@ -223,14 +217,8 @@ public final class AnalysisRegistry implements Closeable {
* @return {@link TokenFilterFactory} provider or <code>null</code> * @return {@link TokenFilterFactory} provider or <code>null</code>
*/ */
public AnalysisProvider<TokenFilterFactory> getTokenFilterProvider(String tokenFilter, IndexSettings indexSettings) { public AnalysisProvider<TokenFilterFactory> getTokenFilterProvider(String tokenFilter, IndexSettings indexSettings) {
final Map<String, Settings> tokenFilterSettings = indexSettings.getSettings().getGroups("index.analysis.filter"); return getProvider(Component.FILTER, tokenFilter, indexSettings, "index.analysis.filter", tokenFilters,
if (tokenFilterSettings.containsKey(tokenFilter)) { this::getTokenFilterProvider);
Settings currentSettings = tokenFilterSettings.get(tokenFilter);
String typeName = currentSettings.get("type");
return getAnalysisProvider(Component.FILTER, tokenFilters, tokenFilter, typeName);
} else {
return getTokenFilterProvider(tokenFilter);
}
} }
/** /**
@ -242,12 +230,18 @@ public final class AnalysisRegistry implements Closeable {
* @return {@link CharFilterFactory} provider or <code>null</code> * @return {@link CharFilterFactory} provider or <code>null</code>
*/ */
public AnalysisProvider<CharFilterFactory> getCharFilterProvider(String charFilter, IndexSettings indexSettings) { public AnalysisProvider<CharFilterFactory> getCharFilterProvider(String charFilter, IndexSettings indexSettings) {
final Map<String, Settings> tokenFilterSettings = indexSettings.getSettings().getGroups("index.analysis.char_filter"); return getProvider(Component.CHAR_FILTER, charFilter, indexSettings, "index.analysis.char_filter", charFilters,
if (tokenFilterSettings.containsKey(charFilter)) { this::getCharFilterProvider);
Settings currentSettings = tokenFilterSettings.get(charFilter); }
return getAnalysisProvider(Component.CHAR_FILTER, charFilters, charFilter, currentSettings.get("type"));
private <T> AnalysisProvider<T> getProvider(Component componentType, String componentName, IndexSettings indexSettings,
String componentSettings, Map<String, AnalysisProvider<T>> providers, Function<String, AnalysisProvider<T>> providerFunction) {
final Map<String, Settings> subSettings = indexSettings.getSettings().getGroups(componentSettings);
if (subSettings.containsKey(componentName)) {
Settings currentSettings = subSettings.get(componentName);
return getAnalysisProvider(componentType, providers, componentName, currentSettings.get("type"));
} else { } else {
return getCharFilterProvider(charFilter); return providerFunction.apply(componentName);
} }
} }
@ -326,9 +320,9 @@ public final class AnalysisRegistry implements Closeable {
} }
// go over the char filters in the bindings and register the ones that are not configured // go over the char filters in the bindings and register the ones that are not configured
for (Map.Entry<String, ? extends AnalysisModule.AnalysisProvider<T>> entry : providerMap.entrySet()) { for (Map.Entry<String, ? extends AnalysisProvider<T>> entry : providerMap.entrySet()) {
String name = entry.getKey(); String name = entry.getKey();
AnalysisModule.AnalysisProvider<T> provider = entry.getValue(); AnalysisProvider<T> provider = entry.getValue();
// we don't want to re-register one that already exists // we don't want to re-register one that already exists
if (settingsMap.containsKey(name)) { if (settingsMap.containsKey(name)) {
continue; continue;
@ -337,7 +331,7 @@ public final class AnalysisRegistry implements Closeable {
if (provider.requiresAnalysisSettings()) { if (provider.requiresAnalysisSettings()) {
continue; continue;
} }
AnalysisModule.AnalysisProvider<T> defaultProvider = defaultInstance.get(name); AnalysisProvider<T> defaultProvider = defaultInstance.get(name);
final T instance; final T instance;
if (defaultProvider == null) { if (defaultProvider == null) {
instance = provider.get(settings, environment, name, defaultSettings); instance = provider.get(settings, environment, name, defaultSettings);
@ -347,20 +341,15 @@ public final class AnalysisRegistry implements Closeable {
factories.put(name, instance); factories.put(name, instance);
} }
for (Map.Entry<String, ? extends AnalysisModule.AnalysisProvider<T>> entry : defaultInstance.entrySet()) { for (Map.Entry<String, ? extends AnalysisProvider<T>> entry : defaultInstance.entrySet()) {
final String name = entry.getKey(); final String name = entry.getKey();
final AnalysisModule.AnalysisProvider<T> provider = entry.getValue(); final AnalysisProvider<T> provider = entry.getValue();
if (factories.containsKey(name) == false) { factories.putIfAbsent(name, provider.get(settings, environment, name, defaultSettings));
final T instance = provider.get(settings, environment, name, defaultSettings);
if (factories.containsKey(name) == false) {
factories.put(name, instance);
}
}
} }
return factories; return factories;
} }
private <T> AnalysisProvider<T> getAnalysisProvider(Component component, Map<String, ? extends AnalysisProvider<T>> providerMap, private static <T> AnalysisProvider<T> getAnalysisProvider(Component component, Map<String, ? extends AnalysisProvider<T>> providerMap,
String name, String typeName) { String name, String typeName) {
if (typeName == null) { if (typeName == null) {
throw new IllegalArgumentException(component + " [" + name + "] must specify either an analyzer type, or a tokenizer"); throw new IllegalArgumentException(component + " [" + name + "] must specify either an analyzer type, or a tokenizer");
@ -374,7 +363,7 @@ public final class AnalysisRegistry implements Closeable {
private static class PrebuiltAnalysis implements Closeable { private static class PrebuiltAnalysis implements Closeable {
final Map<String, AnalysisModule.AnalysisProvider<AnalyzerProvider<?>>> analyzerProviderFactories; final Map<String, AnalysisProvider<AnalyzerProvider<?>>> analyzerProviderFactories;
final Map<String, ? extends AnalysisProvider<TokenFilterFactory>> preConfiguredTokenFilters; final Map<String, ? extends AnalysisProvider<TokenFilterFactory>> preConfiguredTokenFilters;
final Map<String, ? extends AnalysisProvider<TokenizerFactory>> preConfiguredTokenizers; final Map<String, ? extends AnalysisProvider<TokenizerFactory>> preConfiguredTokenizers;
final Map<String, ? extends AnalysisProvider<CharFilterFactory>> preConfiguredCharFilterFactories; final Map<String, ? extends AnalysisProvider<CharFilterFactory>> preConfiguredCharFilterFactories;
@ -399,19 +388,19 @@ public final class AnalysisRegistry implements Closeable {
this.preConfiguredTokenizers = preConfiguredTokenizers; this.preConfiguredTokenizers = preConfiguredTokenizers;
} }
public AnalysisModule.AnalysisProvider<CharFilterFactory> getCharFilterFactory(String name) { public AnalysisProvider<CharFilterFactory> getCharFilterFactory(String name) {
return preConfiguredCharFilterFactories.get(name); return preConfiguredCharFilterFactories.get(name);
} }
public AnalysisModule.AnalysisProvider<TokenFilterFactory> getTokenFilterFactory(String name) { public AnalysisProvider<TokenFilterFactory> getTokenFilterFactory(String name) {
return preConfiguredTokenFilters.get(name); return preConfiguredTokenFilters.get(name);
} }
public AnalysisModule.AnalysisProvider<TokenizerFactory> getTokenizerFactory(String name) { public AnalysisProvider<TokenizerFactory> getTokenizerFactory(String name) {
return preConfiguredTokenizers.get(name); return preConfiguredTokenizers.get(name);
} }
public AnalysisModule.AnalysisProvider<AnalyzerProvider<?>> getAnalyzerProvider(String name) { public AnalysisProvider<AnalyzerProvider<?>> getAnalyzerProvider(String name) {
return analyzerProviderFactories.get(name); return analyzerProviderFactories.get(name);
} }
@ -429,8 +418,6 @@ public final class AnalysisRegistry implements Closeable {
Map<String, CharFilterFactory> charFilterFactoryFactories, Map<String, CharFilterFactory> charFilterFactoryFactories,
Map<String, TokenFilterFactory> tokenFilterFactoryFactories) { Map<String, TokenFilterFactory> tokenFilterFactoryFactories) {
Index index = indexSettings.getIndex();
analyzerProviders = new HashMap<>(analyzerProviders);
Map<String, NamedAnalyzer> analyzers = new HashMap<>(); Map<String, NamedAnalyzer> analyzers = new HashMap<>();
Map<String, NamedAnalyzer> normalizers = new HashMap<>(); Map<String, NamedAnalyzer> normalizers = new HashMap<>();
Map<String, NamedAnalyzer> whitespaceNormalizers = new HashMap<>(); Map<String, NamedAnalyzer> whitespaceNormalizers = new HashMap<>();
@ -461,7 +448,7 @@ public final class AnalysisRegistry implements Closeable {
if (analyzers.containsKey("default_index")) { if (analyzers.containsKey("default_index")) {
throw new IllegalArgumentException("setting [index.analysis.analyzer.default_index] is not supported anymore, use " + throw new IllegalArgumentException("setting [index.analysis.analyzer.default_index] is not supported anymore, use " +
"[index.analysis.analyzer.default] instead for index [" + index.getName() + "]"); "[index.analysis.analyzer.default] instead for index [" + indexSettings.getIndex().getName() + "]");
} }
for (Map.Entry<String, NamedAnalyzer> analyzer : analyzers.entrySet()) { for (Map.Entry<String, NamedAnalyzer> analyzer : analyzers.entrySet()) {