Skip to content

Commit

Permalink
Change IndexAnalyzers default analyzer access (#42011)
Browse files Browse the repository at this point in the history
Currently IndexAnalyzers keeps the three default as separate class members
although they should refer to the same analyzers held in the additional
analyzers map under the default names. This assumption should be made more
explicit by keeping all analyzers in the map. This change adapts the constructor
to check all the default entries are there and the getters to reach into the map
with the default names when needed.
  • Loading branch information
Christoph Büscher committed May 10, 2019
1 parent 31a7df8 commit eb42fa8
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public Set<Entry<String, NamedAnalyzer>> entrySet() {
}
};
try (IndexAnalyzers fakeIndexAnalzyers =
new IndexAnalyzers(indexSettings, fakeDefault, fakeDefault, fakeDefault, analyzerMap, analyzerMap, analyzerMap)) {
new IndexAnalyzers(indexSettings, analyzerMap, analyzerMap, analyzerMap)) {
MapperService mapperService = new MapperService(indexSettings, fakeIndexAnalzyers, xContentRegistry, similarityService,
mapperRegistry, () -> null);
mapperService.merge(indexMetaData, MapperService.MergeReason.MAPPING_RECOVERY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public final class AnalysisRegistry implements Closeable {
public static final String INDEX_ANALYSIS_CHAR_FILTER = "index.analysis.char_filter";
public static final String INDEX_ANALYSIS_FILTER = "index.analysis.filter";
public static final String INDEX_ANALYSIS_TOKENIZER = "index.analysis.tokenizer";

public static final String DEFAULT_ANALYZER_NAME = "default";
public static final String DEFAULT_SEARCH_ANALYZER_NAME = "default_search";
public static final String DEFAULT_SEARCH_QUOTED_ANALYZER_NAME = "default_search_quoted";

private final PrebuiltAnalysis prebuiltAnalysis;
private final Map<String, Analyzer> cachedAnalyzer = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -439,37 +444,29 @@ public IndexAnalyzers build(IndexSettings indexSettings,
"whitespace", () -> new WhitespaceTokenizer(), tokenFilterFactoryFactories, charFilterFactoryFactories);
}

if (!analyzers.containsKey("default")) {
NamedAnalyzer defaultAnalyzer = produceAnalyzer("default", new StandardAnalyzerProvider(indexSettings, null, "default",
Settings.Builder.EMPTY_SETTINGS), tokenFilterFactoryFactories, charFilterFactoryFactories, tokenizerFactoryFactories);
analyzers.put("default", defaultAnalyzer);
}
if (!analyzers.containsKey("default_search")) {
analyzers.put("default_search", analyzers.get("default"));
if (!analyzers.containsKey(DEFAULT_ANALYZER_NAME)) {
analyzers.put(DEFAULT_ANALYZER_NAME,
produceAnalyzer(DEFAULT_ANALYZER_NAME,
new StandardAnalyzerProvider(indexSettings, null, DEFAULT_ANALYZER_NAME, Settings.Builder.EMPTY_SETTINGS),
tokenFilterFactoryFactories, charFilterFactoryFactories, tokenizerFactoryFactories));
}
if (!analyzers.containsKey("default_search_quoted")) {
analyzers.put("default_search_quoted", analyzers.get("default_search"));
}

NamedAnalyzer defaultAnalyzer = analyzers.get("default");
NamedAnalyzer defaultAnalyzer = analyzers.get(DEFAULT_ANALYZER_NAME);
if (defaultAnalyzer == null) {
throw new IllegalArgumentException("no default analyzer configured");
}
defaultAnalyzer.checkAllowedInMode(AnalysisMode.ALL);

if (analyzers.containsKey("default_index")) {
throw new IllegalArgumentException("setting [index.analysis.analyzer.default_index] is not supported anymore, use " +
"[index.analysis.analyzer.default] instead for index [" + index.getName() + "]");
}
NamedAnalyzer defaultSearchAnalyzer = analyzers.getOrDefault("default_search", defaultAnalyzer);
NamedAnalyzer defaultSearchQuoteAnalyzer = analyzers.getOrDefault("default_search_quote", defaultSearchAnalyzer);

for (Map.Entry<String, NamedAnalyzer> analyzer : analyzers.entrySet()) {
if (analyzer.getKey().startsWith("_")) {
throw new IllegalArgumentException("analyzer name must not start with '_'. got \"" + analyzer.getKey() + "\"");
}
}
return new IndexAnalyzers(indexSettings, defaultAnalyzer, defaultSearchAnalyzer, defaultSearchQuoteAnalyzer, analyzers, normalizers,
whitespaceNormalizers);
return new IndexAnalyzers(indexSettings, analyzers, normalizers, whitespaceNormalizers);
}

private static NamedAnalyzer produceAnalyzer(String name, AnalyzerProvider<?> analyzerFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
import java.io.Closeable;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import static java.util.Collections.unmodifiableMap;
import static org.elasticsearch.index.analysis.AnalysisRegistry.DEFAULT_ANALYZER_NAME;
import static org.elasticsearch.index.analysis.AnalysisRegistry.DEFAULT_SEARCH_ANALYZER_NAME;
import static org.elasticsearch.index.analysis.AnalysisRegistry.DEFAULT_SEARCH_QUOTED_ANALYZER_NAME;

/**
* IndexAnalyzers contains a name to analyzer mapping for a specific index.
Expand All @@ -37,23 +41,18 @@
* @see AnalysisRegistry
*/
public final class IndexAnalyzers extends AbstractIndexComponent implements Closeable {
private final NamedAnalyzer defaultIndexAnalyzer;
private final NamedAnalyzer defaultSearchAnalyzer;
private final NamedAnalyzer defaultSearchQuoteAnalyzer;
private final Map<String, NamedAnalyzer> analyzers;
private final Map<String, NamedAnalyzer> normalizers;
private final Map<String, NamedAnalyzer> whitespaceNormalizers;

public IndexAnalyzers(IndexSettings indexSettings, NamedAnalyzer defaultIndexAnalyzer, NamedAnalyzer defaultSearchAnalyzer,
NamedAnalyzer defaultSearchQuoteAnalyzer, Map<String, NamedAnalyzer> analyzers,
Map<String, NamedAnalyzer> normalizers, Map<String, NamedAnalyzer> whitespaceNormalizers) {
public IndexAnalyzers(IndexSettings indexSettings, Map<String, NamedAnalyzer> analyzers, Map<String, NamedAnalyzer> normalizers,
Map<String, NamedAnalyzer> whitespaceNormalizers) {
super(indexSettings);
if (defaultIndexAnalyzer.name().equals("default") == false) {
throw new IllegalStateException("default analyzer must have the name [default] but was: [" + defaultIndexAnalyzer.name() + "]");
Objects.requireNonNull(analyzers.get(DEFAULT_ANALYZER_NAME), "the default analyzer must be set");
if (analyzers.get(DEFAULT_ANALYZER_NAME).name().equals(DEFAULT_ANALYZER_NAME) == false) {
throw new IllegalStateException(
"default analyzer must have the name [default] but was: [" + analyzers.get(DEFAULT_ANALYZER_NAME).name() + "]");
}
this.defaultIndexAnalyzer = defaultIndexAnalyzer;
this.defaultSearchAnalyzer = defaultSearchAnalyzer;
this.defaultSearchQuoteAnalyzer = defaultSearchQuoteAnalyzer;
this.analyzers = unmodifiableMap(analyzers);
this.normalizers = unmodifiableMap(normalizers);
this.whitespaceNormalizers = unmodifiableMap(whitespaceNormalizers);
Expand Down Expand Up @@ -84,21 +83,21 @@ public NamedAnalyzer getWhitespaceNormalizer(String name) {
* Returns the default index analyzer for this index
*/
public NamedAnalyzer getDefaultIndexAnalyzer() {
return defaultIndexAnalyzer;
return analyzers.get(DEFAULT_ANALYZER_NAME);
}

/**
* Returns the default search analyzer for this index
* Returns the default search analyzer for this index. If not set, this will return the default analyzer
*/
public NamedAnalyzer getDefaultSearchAnalyzer() {
return defaultSearchAnalyzer;
return analyzers.getOrDefault(DEFAULT_SEARCH_ANALYZER_NAME, getDefaultIndexAnalyzer());
}

/**
* Returns the default search quote analyzer for this index
*/
public NamedAnalyzer getDefaultSearchQuoteAnalyzer() {
return defaultSearchQuoteAnalyzer;
return analyzers.getOrDefault(DEFAULT_SEARCH_QUOTED_ANALYZER_NAME, getDefaultSearchAnalyzer());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public TokenStream create(TokenStream tokenStream) {
Analyzer analyzer = new CustomAnalyzer("tokenizerName", null, new CharFilterFactory[0], new TokenFilterFactory[] { tokenFilter });
MapperException ex = expectThrows(MapperException.class,
() -> emptyRegistry.build(IndexSettingsModule.newIndexSettings("index", settings),
singletonMap("default", new PreBuiltAnalyzerProvider("my_analyzer", AnalyzerScope.INDEX, analyzer)), emptyMap(),
singletonMap("default", new PreBuiltAnalyzerProvider("default", AnalyzerScope.INDEX, analyzer)), emptyMap(),
emptyMap(), emptyMap(), emptyMap()));
assertEquals("analyzer [my_analyzer] 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 testOverrideDefaultIndexAnalyzerIsUnsupported() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.standard.StandardAnalyzer;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.IndexSettingsModule;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class IndexAnalyzersTests extends ESTestCase {

/**
* test the checks in the constructor
*/
public void testAnalyzerMapChecks() {
Map<String, NamedAnalyzer> analyzers = new HashMap<>();
{
NullPointerException ex = expectThrows(NullPointerException.class,
() -> new IndexAnalyzers(IndexSettingsModule.newIndexSettings("index", Settings.EMPTY), analyzers,
Collections.emptyMap(), Collections.emptyMap()));
assertEquals("the default analyzer must be set", ex.getMessage());
}
{
analyzers.put(AnalysisRegistry.DEFAULT_ANALYZER_NAME,
new NamedAnalyzer("otherName", AnalyzerScope.INDEX, new StandardAnalyzer()));
IllegalStateException ex = expectThrows(IllegalStateException.class,
() -> new IndexAnalyzers(IndexSettingsModule.newIndexSettings("index", Settings.EMPTY), analyzers,
Collections.emptyMap(), Collections.emptyMap()));
assertEquals("default analyzer must have the name [default] but was: [otherName]", ex.getMessage());
}
}

public void testAnalyzerDefaults() throws IOException {
Map<String, NamedAnalyzer> analyzers = new HashMap<>();
NamedAnalyzer analyzer = new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer());
analyzers.put(AnalysisRegistry.DEFAULT_ANALYZER_NAME, analyzer);

// if only "default" is set in the map, all getters should return the same analyzer
try (IndexAnalyzers indexAnalyzers = new IndexAnalyzers(IndexSettingsModule.newIndexSettings("index", Settings.EMPTY), analyzers,
Collections.emptyMap(), Collections.emptyMap())) {
assertSame(analyzer, indexAnalyzers.getDefaultIndexAnalyzer());
assertSame(analyzer, indexAnalyzers.getDefaultSearchAnalyzer());
assertSame(analyzer, indexAnalyzers.getDefaultSearchQuoteAnalyzer());
}

analyzers.put(AnalysisRegistry.DEFAULT_SEARCH_ANALYZER_NAME,
new NamedAnalyzer("my_search_analyzer", AnalyzerScope.INDEX, new StandardAnalyzer()));
try (IndexAnalyzers indexAnalyzers = new IndexAnalyzers(IndexSettingsModule.newIndexSettings("index", Settings.EMPTY), analyzers,
Collections.emptyMap(), Collections.emptyMap())) {
assertSame(analyzer, indexAnalyzers.getDefaultIndexAnalyzer());
assertEquals("my_search_analyzer", indexAnalyzers.getDefaultSearchAnalyzer().name());
assertEquals("my_search_analyzer", indexAnalyzers.getDefaultSearchQuoteAnalyzer().name());
}

analyzers.put(AnalysisRegistry.DEFAULT_SEARCH_QUOTED_ANALYZER_NAME,
new NamedAnalyzer("my_search_quote_analyzer", AnalyzerScope.INDEX, new StandardAnalyzer()));
try (IndexAnalyzers indexAnalyzers = new IndexAnalyzers(IndexSettingsModule.newIndexSettings("index", Settings.EMPTY), analyzers,
Collections.emptyMap(), Collections.emptyMap())) {
assertSame(analyzer, indexAnalyzers.getDefaultIndexAnalyzer());
assertEquals("my_search_analyzer", indexAnalyzers.getDefaultSearchAnalyzer().name());
assertEquals("my_search_quote_analyzer", indexAnalyzers.getDefaultSearchQuoteAnalyzer().name());
}
}

}
Loading

0 comments on commit eb42fa8

Please sign in to comment.