Skip to content

Commit 5ba06b6

Browse files
author
Ali Beyad
authored
Removes support for adding aliases to analyzers. Indices created pre 5.x (#19994)
that have analyzer aliases in their analysis settings will still work, but any attempts to create an alias for analyzers in newly created indices will result in an IllegalArgumentException. As a result, the setting `index.analysis.analyzer.{analyzerName}.alias` is no longer supported. Closes #18244
1 parent 84bf24b commit 5ba06b6

File tree

5 files changed

+44
-74
lines changed

5 files changed

+44
-74
lines changed

core/src/main/java/org/elasticsearch/index/analysis/AnalysisService.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@
2121

2222
import org.apache.lucene.analysis.Analyzer;
2323
import org.elasticsearch.Version;
24-
import org.elasticsearch.common.Strings;
2524
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.common.util.set.Sets;
2626
import org.elasticsearch.index.AbstractIndexComponent;
2727
import org.elasticsearch.index.IndexSettings;
2828
import org.elasticsearch.index.mapper.TextFieldMapper;
2929

3030
import java.io.Closeable;
31-
import java.util.Arrays;
3231
import java.util.HashMap;
33-
import java.util.HashSet;
3432
import java.util.Map;
3533
import java.util.Set;
3634

@@ -151,16 +149,22 @@ private void processAnalyzerFactory(String name, AnalyzerProvider<?> analyzerFac
151149
throw new IllegalStateException("already registered analyzer with name: " + name);
152150
}
153151
analyzers.put(name, analyzer);
154-
String strAliases = this.indexSettings.getSettings().get("index.analysis.analyzer." + analyzerFactory.name() + ".alias");
155-
Set<String> aliases = new HashSet<>();
156-
if (strAliases != null) {
157-
aliases.addAll(Strings.commaDelimitedListToSet(strAliases));
158-
}
159-
aliases.addAll(Arrays.asList(this.indexSettings.getSettings()
160-
.getAsArray("index.analysis.analyzer." + analyzerFactory.name() + ".alias")));
161-
for (String alias : aliases) {
162-
if (analyzerAliases.putIfAbsent(alias, analyzer) != null) {
163-
throw new IllegalStateException("alias [" + alias + "] is already used by [" + analyzerAliases.get(alias).name() + "]");
152+
// TODO: remove alias support completely when we no longer support pre 5.0 indices
153+
final String analyzerAliasKey = "index.analysis.analyzer." + analyzerFactory.name() + ".alias";
154+
if (indexSettings.getSettings().get(analyzerAliasKey) != null) {
155+
if (indexSettings.getIndexVersionCreated().onOrAfter(Version.V_5_0_0_alpha6)) {
156+
// do not allow alias creation if the index was created on or after v5.0 alpha6
157+
throw new IllegalArgumentException("setting [" + analyzerAliasKey + "] is not supported");
158+
}
159+
160+
// the setting is now removed but we only support it for loading indices created before v5.0
161+
deprecationLogger.deprecated("setting [{}] is only allowed on index [{}] because it was created before 5.x; " +
162+
"analyzer aliases can no longer be created on new indices.", analyzerAliasKey, index().getName());
163+
Set<String> aliases = Sets.newHashSet(indexSettings.getSettings().getAsArray(analyzerAliasKey));
164+
for (String alias : aliases) {
165+
if (analyzerAliases.putIfAbsent(alias, analyzer) != null) {
166+
throw new IllegalStateException("alias [" + alias + "] is already used by [" + analyzerAliases.get(alias).name() + "]");
167+
}
164168
}
165169
}
166170
}
@@ -174,6 +178,9 @@ public void close() {
174178
} catch (NullPointerException e) {
175179
// because analyzers are aliased, they might be closed several times
176180
// an NPE is thrown in this case, so ignore....
181+
// TODO: Analyzer's can no longer have aliases in indices created in 5.x and beyond,
182+
// so we only allow the aliases for analyzers on indices created pre 5.x for backwards
183+
// compatibility. Once pre 5.0 indices are no longer supported, this check should be removed.
177184
} catch (Exception e) {
178185
logger.debug("failed to close analyzer {}", analyzer);
179186
}

core/src/test/java/org/elasticsearch/indices/analysis/AnalysisModuleTests.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ public void testAnalyzerAlias() throws IOException {
132132
.put("index.analysis.analyzer.foobar_search.alias","default_search")
133133
.put("index.analysis.analyzer.foobar_search.type","english")
134134
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
135-
.put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersion(random()))
135+
// analyzer aliases are only allowed in 2.x indices
136+
.put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.V_2_3_5))
136137
.build();
137138
AnalysisRegistry newRegistry = getNewRegistry(settings);
138139
AnalysisService as = getAnalysisService(newRegistry, settings);
@@ -147,7 +148,8 @@ public void testAnalyzerAliasReferencesAlias() throws IOException {
147148
.put("index.analysis.analyzer.foobar_search.alias","default_search")
148149
.put("index.analysis.analyzer.foobar_search.type", "default")
149150
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
150-
.put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersion(random()))
151+
// analyzer aliases are only allowed in 2.x indices
152+
.put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.V_2_3_5))
151153
.build();
152154
AnalysisRegistry newRegistry = getNewRegistry(settings);
153155
AnalysisService as = getAnalysisService(newRegistry, settings);
@@ -161,7 +163,8 @@ public void testAnalyzerAliasDefault() throws IOException {
161163
.put("index.analysis.analyzer.foobar.alias","default")
162164
.put("index.analysis.analyzer.foobar.type", "keyword")
163165
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
164-
.put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersion(random()))
166+
// analyzer aliases are only allowed in 2.x indices
167+
.put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.V_2_3_5))
165168
.build();
166169
AnalysisRegistry newRegistry = getNewRegistry(settings);
167170
AnalysisService as = getAnalysisService(newRegistry, settings);
@@ -175,13 +178,28 @@ public void testAnalyzerAliasMoreThanOnce() throws IOException {
175178
.put("index.analysis.analyzer.foobar.type", "keyword")
176179
.put("index.analysis.analyzer.foobar1.alias","default")
177180
.put("index.analysis.analyzer.foobar1.type", "english")
178-
.put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersion(random()))
181+
// analyzer aliases are only allowed in 2.x indices
182+
.put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.V_2_3_5))
179183
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
180184
.build();
181185
AnalysisRegistry newRegistry = getNewRegistry(settings);
182186
IllegalStateException ise = expectThrows(IllegalStateException.class, () -> getAnalysisService(newRegistry, settings));
183187
assertEquals("alias [default] is already used by [foobar]", ise.getMessage());
184188
}
189+
190+
public void testAnalyzerAliasNotAllowedPost5x() throws IOException {
191+
Settings settings = Settings.builder()
192+
.put("index.analysis.analyzer.foobar.type", "standard")
193+
.put("index.analysis.analyzer.foobar.alias","foobaz")
194+
// analyzer aliases were removed in v5.0.0 alpha6
195+
.put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_5_0_0_alpha6, null))
196+
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
197+
.build();
198+
AnalysisRegistry registry = getNewRegistry(settings);
199+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> getAnalysisService(registry, settings));
200+
assertEquals("setting [index.analysis.analyzer.foobar.alias] is not supported", e.getMessage());
201+
}
202+
185203
public void testVersionedAnalyzers() throws Exception {
186204
String yaml = "/org/elasticsearch/index/analysis/test1.yml";
187205
Settings settings2 = Settings.builder()
@@ -246,10 +264,6 @@ private void testSimpleConfiguration(Settings settings) throws IOException {
246264
CustomAnalyzer custom5 = (CustomAnalyzer) analyzer;
247265
assertThat(custom5.charFilters()[0], instanceOf(MappingCharFilterFactory.class));
248266

249-
// verify aliases
250-
analyzer = analysisService.analyzer("alias1").analyzer();
251-
assertThat(analyzer, instanceOf(StandardAnalyzer.class));
252-
253267
// check custom pattern replace filter
254268
analyzer = analysisService.analyzer("custom3").analyzer();
255269
assertThat(analyzer, instanceOf(CustomAnalyzer.class));
@@ -332,7 +346,8 @@ public void testUnderscoreInAnalyzerNameAlias() throws IOException {
332346
.put("index.analysis.analyzer.valid_name.tokenizer", "keyword")
333347
.put("index.analysis.analyzer.valid_name.alias", "_invalid_name")
334348
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
335-
.put(IndexMetaData.SETTING_VERSION_CREATED, "1")
349+
// analyzer aliases are only allowed for 2.x indices
350+
.put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.V_2_3_5))
336351
.build();
337352
try {
338353
getAnalysisService(settings);

core/src/test/resources/org/elasticsearch/index/analysis/test1.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@
4141
},
4242
"analyzer":{
4343
"standard":{
44-
"alias":"alias1,alias2",
4544
"type":"standard",
4645
"stopwords":["test1", "test2", "test3"]
4746
},
4847
"custom1":{
49-
"alias":["alias4", "alias5"],
5048
"tokenizer":"standard",
5149
"filter":["stop", "stop2"]
5250
},

core/src/test/resources/org/elasticsearch/index/analysis/test1.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ index :
2929
word_list : [donau, dampf, schiff, spargel, creme, suppe]
3030
analyzer :
3131
standard :
32-
alias: alias1,alias2
3332
type : standard
3433
stopwords : [test1, test2, test3]
3534
custom1 :
36-
alias : [alias4, alias5]
3735
tokenizer : standard
3836
filter : [stop, stop2]
3937
custom2 :

rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_template/10_basic.yaml

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -70,51 +70,3 @@
7070
settings:
7171
number_of_shards: 1
7272
number_of_replicas: 0
73-
---
74-
"Put template with analyzer alias":
75-
76-
- do:
77-
indices.put_template:
78-
name: test
79-
create: true
80-
order: 0
81-
body:
82-
template: test_*
83-
settings:
84-
index.analysis.analyzer.foobar.alias: "default"
85-
index.analysis.analyzer.foobar.type: "keyword"
86-
index.analysis.analyzer.foobar_search.alias: "default_search"
87-
index.analysis.analyzer.foobar_search.type: "standard"
88-
89-
- do:
90-
index:
91-
index: test_index
92-
type: test
93-
body: { field: "the quick brown fox" }
94-
95-
- do:
96-
indices.refresh:
97-
index: test_index
98-
99-
- do:
100-
search:
101-
index: test_index
102-
type: test
103-
body:
104-
query:
105-
term:
106-
field: "the quick brown fox"
107-
108-
- match: {hits.total: 1}
109-
110-
- do:
111-
search:
112-
index: test_index
113-
type: test
114-
body:
115-
query:
116-
match:
117-
field: "the quick brown fox"
118-
119-
- match: {hits.total: 0}
120-

0 commit comments

Comments
 (0)