Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change IndexAnalyzers default analyzer access #42011

Merged
merged 4 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ protected TokenStreamComponents createComponents(String fieldName) {
});

final Map<String, NamedAnalyzer> analyzerMap = new AbstractMap<String, NamedAnalyzer>() {

@Override
public boolean containsKey(Object key) {
assert key instanceof String : "key must be a string but was: " + key.getClass();
return true;
}

@Override
public NamedAnalyzer get(Object key) {
assert key instanceof String : "key must be a string but was: " + key.getClass();
Expand All @@ -195,7 +202,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,35 @@ 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_search_quoted")) {
analyzers.put("default_search_quoted", analyzers.get("default_search"));
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));
}

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_SEARCH_ANALYZER_NAME)) {
analyzers.put(DEFAULT_SEARCH_ANALYZER_NAME, analyzers.get(DEFAULT_ANALYZER_NAME));
}
if (!analyzers.containsKey(DEFAULT_SEARCH_QUOTED_ANALYZER_NAME)) {
analyzers.put(DEFAULT_SEARCH_QUOTED_ANALYZER_NAME, analyzers.get(DEFAULT_SEARCH_ANALYZER_NAME));
}

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 @@ -28,6 +28,9 @@
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 +40,22 @@
* @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) {
// check the defaults are set, although the values might be null (except for the default analyzer which needs to be set)
assert analyzers.containsKey(DEFAULT_ANALYZER_NAME);
assert analyzers.containsKey(DEFAULT_SEARCH_ANALYZER_NAME);
assert analyzers.containsKey(DEFAULT_SEARCH_QUOTED_ANALYZER_NAME);
NamedAnalyzer defaultIndexAnalyzer = analyzers.get(DEFAULT_ANALYZER_NAME);
assert defaultIndexAnalyzer != null : "the default analyzer must be set";
if (defaultIndexAnalyzer.name().equals(DEFAULT_ANALYZER_NAME) == false) {
throw new IllegalStateException("default analyzer must have the name [default] but was: [" + defaultIndexAnalyzer.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 +86,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
*/
public NamedAnalyzer getDefaultSearchAnalyzer() {
return defaultSearchAnalyzer;
return analyzers.get(DEFAULT_SEARCH_ANALYZER_NAME);
}

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

@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
Expand Up @@ -40,6 +40,9 @@
import java.util.HashMap;
import java.util.Map;

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;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -57,29 +60,35 @@ public void testParseTextFieldCheckAnalyzerAnalysisMode() {
Mapper.TypeParser.ParserContext parserContext = mock(Mapper.TypeParser.ParserContext.class);

// check AnalysisMode.ALL works
Map<String, NamedAnalyzer> analyzers = new HashMap<>();
Map<String, NamedAnalyzer> analyzers = defaultAnalyzers();
analyzers.put("my_analyzer",
new NamedAnalyzer("my_named_analyzer", AnalyzerScope.INDEX, createAnalyzerWithMode("my_analyzer", AnalysisMode.ALL)));

IndexAnalyzers indexAnalyzers = new IndexAnalyzers(indexSettings, new NamedAnalyzer("default", AnalyzerScope.INDEX, null), null,
null, analyzers, Collections.emptyMap(), Collections.emptyMap());
IndexAnalyzers indexAnalyzers = new IndexAnalyzers(indexSettings, analyzers, Collections.emptyMap(), Collections.emptyMap());
when(parserContext.getIndexAnalyzers()).thenReturn(indexAnalyzers);
TypeParsers.parseTextField(builder, "name", new HashMap<>(fieldNode), parserContext);

// check that "analyzer" set to something that only supports AnalysisMode.SEARCH_TIME or AnalysisMode.INDEX_TIME is blocked
AnalysisMode mode = randomFrom(AnalysisMode.SEARCH_TIME, AnalysisMode.INDEX_TIME);
analyzers = new HashMap<>();
analyzers = defaultAnalyzers();
analyzers.put("my_analyzer", new NamedAnalyzer("my_named_analyzer", AnalyzerScope.INDEX,
createAnalyzerWithMode("my_analyzer", mode)));
indexAnalyzers = new IndexAnalyzers(indexSettings, new NamedAnalyzer("default", AnalyzerScope.INDEX, null), null, null, analyzers,
Collections.emptyMap(), Collections.emptyMap());
indexAnalyzers = new IndexAnalyzers(indexSettings, analyzers, Collections.emptyMap(), Collections.emptyMap());
when(parserContext.getIndexAnalyzers()).thenReturn(indexAnalyzers);
MapperException ex = expectThrows(MapperException.class,
() -> TypeParsers.parseTextField(builder, "name", new HashMap<>(fieldNode), parserContext));
assertEquals("analyzer [my_named_analyzer] contains filters [my_analyzer] that are not allowed to run in all mode.",
ex.getMessage());
}

private static Map<String, NamedAnalyzer> defaultAnalyzers() {
Map<String, NamedAnalyzer> analyzers = new HashMap<>();
analyzers.put(DEFAULT_ANALYZER_NAME, new NamedAnalyzer("default", AnalyzerScope.INDEX, null));
analyzers.put(DEFAULT_SEARCH_ANALYZER_NAME, new NamedAnalyzer("default", AnalyzerScope.INDEX, null));
analyzers.put(DEFAULT_SEARCH_QUOTED_ANALYZER_NAME, new NamedAnalyzer("default", AnalyzerScope.INDEX, null));
return analyzers;
}

public void testParseTextFieldCheckSearchAnalyzerAnalysisMode() {
TextFieldMapper.Builder builder = new TextFieldMapper.Builder("textField");
for (String settingToTest : new String[] { "search_analyzer", "search_quote_analyzer" }) {
Expand All @@ -92,25 +101,23 @@ public void testParseTextFieldCheckSearchAnalyzerAnalysisMode() {
Mapper.TypeParser.ParserContext parserContext = mock(Mapper.TypeParser.ParserContext.class);

// check AnalysisMode.ALL and AnalysisMode.SEARCH_TIME works
Map<String, NamedAnalyzer> analyzers = new HashMap<>();
Map<String, NamedAnalyzer> analyzers = defaultAnalyzers();
AnalysisMode mode = randomFrom(AnalysisMode.ALL, AnalysisMode.SEARCH_TIME);
analyzers.put("my_analyzer",
new NamedAnalyzer("my_named_analyzer", AnalyzerScope.INDEX, createAnalyzerWithMode("my_analyzer", mode)));
analyzers.put("standard", new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer()));

IndexAnalyzers indexAnalyzers = new IndexAnalyzers(indexSettings, new NamedAnalyzer("default", AnalyzerScope.INDEX, null), null,
null, analyzers, Collections.emptyMap(), Collections.emptyMap());
IndexAnalyzers indexAnalyzers = new IndexAnalyzers(indexSettings, analyzers, Collections.emptyMap(), Collections.emptyMap());
when(parserContext.getIndexAnalyzers()).thenReturn(indexAnalyzers);
TypeParsers.parseTextField(builder, "name", new HashMap<>(fieldNode), parserContext);

// check that "analyzer" set to AnalysisMode.INDEX_TIME is blocked
mode = AnalysisMode.INDEX_TIME;
analyzers = new HashMap<>();
analyzers = defaultAnalyzers();
analyzers.put("my_analyzer",
new NamedAnalyzer("my_named_analyzer", AnalyzerScope.INDEX, createAnalyzerWithMode("my_analyzer", mode)));
analyzers.put("standard", new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer()));
indexAnalyzers = new IndexAnalyzers(indexSettings, new NamedAnalyzer("default", AnalyzerScope.INDEX, null), null, null,
analyzers, Collections.emptyMap(), Collections.emptyMap());
indexAnalyzers = new IndexAnalyzers(indexSettings, analyzers, Collections.emptyMap(), Collections.emptyMap());
when(parserContext.getIndexAnalyzers()).thenReturn(indexAnalyzers);
MapperException ex = expectThrows(MapperException.class,
() -> TypeParsers.parseTextField(builder, "name", new HashMap<>(fieldNode), parserContext));
Expand All @@ -127,11 +134,10 @@ public void testParseTextFieldCheckAnalyzerWithSearchAnalyzerAnalysisMode() {

// check that "analyzer" set to AnalysisMode.INDEX_TIME is blocked if there is no search analyzer
AnalysisMode mode = AnalysisMode.INDEX_TIME;
Map<String, NamedAnalyzer> analyzers = new HashMap<>();
Map<String, NamedAnalyzer> analyzers = defaultAnalyzers();
analyzers.put("my_analyzer",
new NamedAnalyzer("my_named_analyzer", AnalyzerScope.INDEX, createAnalyzerWithMode("my_analyzer", mode)));
IndexAnalyzers indexAnalyzers = new IndexAnalyzers(indexSettings, new NamedAnalyzer("default", AnalyzerScope.INDEX, null), null,
null, analyzers, Collections.emptyMap(), Collections.emptyMap());
IndexAnalyzers indexAnalyzers = new IndexAnalyzers(indexSettings, analyzers, Collections.emptyMap(), Collections.emptyMap());
when(parserContext.getIndexAnalyzers()).thenReturn(indexAnalyzers);
MapperException ex = expectThrows(MapperException.class,
() -> TypeParsers.parseTextField(builder, "name", new HashMap<>(fieldNode), parserContext));
Expand All @@ -140,14 +146,13 @@ public void testParseTextFieldCheckAnalyzerWithSearchAnalyzerAnalysisMode() {

// check AnalysisMode.INDEX_TIME is okay if search analyzer is also set
fieldNode.put("search_analyzer", "standard");
analyzers = new HashMap<>();
analyzers = defaultAnalyzers();
mode = randomFrom(AnalysisMode.ALL, AnalysisMode.INDEX_TIME);
analyzers.put("my_analyzer",
new NamedAnalyzer("my_named_analyzer", AnalyzerScope.INDEX, createAnalyzerWithMode("my_analyzer", mode)));
analyzers.put("standard", new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer()));

indexAnalyzers = new IndexAnalyzers(indexSettings, new NamedAnalyzer("default", AnalyzerScope.INDEX, null), null, null, analyzers,
Collections.emptyMap(), Collections.emptyMap());
indexAnalyzers = new IndexAnalyzers(indexSettings, analyzers, Collections.emptyMap(), Collections.emptyMap());
when(parserContext.getIndexAnalyzers()).thenReturn(indexAnalyzers);
TypeParsers.parseTextField(builder, "name", new HashMap<>(fieldNode), parserContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.AnalysisRegistry;
import org.elasticsearch.index.analysis.AnalyzerScope;
import org.elasticsearch.index.analysis.IndexAnalyzers;
import org.elasticsearch.index.analysis.NamedAnalyzer;
Expand Down Expand Up @@ -60,9 +61,13 @@ long appliedOperations() {
}

public TranslogHandler(NamedXContentRegistry xContentRegistry, IndexSettings indexSettings) {
NamedAnalyzer defaultAnalyzer = new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer());
IndexAnalyzers indexAnalyzers =
new IndexAnalyzers(indexSettings, defaultAnalyzer, defaultAnalyzer, defaultAnalyzer, emptyMap(), emptyMap(), emptyMap());
Map<String, NamedAnalyzer> analyzers = new HashMap<>();
analyzers.put(AnalysisRegistry.DEFAULT_ANALYZER_NAME, new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer()));
analyzers.put(AnalysisRegistry.DEFAULT_SEARCH_ANALYZER_NAME,
new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer()));
analyzers.put(AnalysisRegistry.DEFAULT_SEARCH_QUOTED_ANALYZER_NAME,
new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer()));
IndexAnalyzers indexAnalyzers = new IndexAnalyzers(indexSettings, analyzers, emptyMap(), emptyMap());
cbuescher marked this conversation as resolved.
Show resolved Hide resolved
SimilarityService similarityService = new SimilarityService(indexSettings, null, emptyMap());
MapperRegistry mapperRegistry = new IndicesModule(emptyList()).getMapperRegistry();
mapperService = new MapperService(indexSettings, indexAnalyzers, xContentRegistry, similarityService, mapperRegistry,
Expand Down