Skip to content

Commit c0d1597

Browse files
authored
Deprecation check for classic similarity (#36577)
Add a deprecation check for indices which have fields and custom similarities that use the `classic` similarity value.
1 parent 8c524aa commit c0d1597

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ private DeprecationChecks() {
5757
IndexDeprecationChecks::percolatorUnmappedFieldsAsStringCheck,
5858
IndexDeprecationChecks::indexNameCheck,
5959
IndexDeprecationChecks::nodeLeftDelayedTimeCheck,
60-
IndexDeprecationChecks::shardOnStartupCheck
60+
IndexDeprecationChecks::shardOnStartupCheck,
61+
IndexDeprecationChecks::classicSimilarityMappingCheck,
62+
IndexDeprecationChecks::classicSimilaritySettingsCheck
6163
));
6264

6365
/**

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Map;
2525
import java.util.function.BiConsumer;
2626
import java.util.function.Function;
27+
import java.util.stream.Collectors;
2728

2829
/**
2930
* Index-specific deprecation checks
@@ -133,7 +134,38 @@ static DeprecationIssue percolatorUnmappedFieldsAsStringCheck(IndexMetaData inde
133134
}
134135
return null;
135136
}
136-
137+
138+
static DeprecationIssue classicSimilarityMappingCheck(IndexMetaData indexMetaData) {
139+
List<String> issues = new ArrayList<>();
140+
fieldLevelMappingIssue(indexMetaData, ((mappingMetaData, sourceAsMap) -> issues.addAll(
141+
findInPropertiesRecursively(mappingMetaData.type(), sourceAsMap,
142+
property -> "classic".equals(property.get("similarity"))))));
143+
if (issues.size() > 0) {
144+
return new DeprecationIssue(DeprecationIssue.Level.WARNING,
145+
"Classic similarity has been removed",
146+
"https://www.elastic.co/guide/en/elasticsearch/reference/master/" +
147+
"#_the_literal_classic_literal_similarity_has_been_removed",
148+
"Fields which use classic similarity: " + issues.toString());
149+
}
150+
return null;
151+
}
152+
153+
static DeprecationIssue classicSimilaritySettingsCheck(IndexMetaData indexMetaData) {
154+
Map<String, Settings> similarities = indexMetaData.getSettings().getGroups("index.similarity");
155+
List<String> classicSimilarities = similarities.entrySet().stream()
156+
.filter(entry -> "classic".equals(entry.getValue().get("type")))
157+
.map(Map.Entry::getKey)
158+
.collect(Collectors.toList());
159+
if (classicSimilarities.size() > 0) {
160+
return new DeprecationIssue(DeprecationIssue.Level.WARNING,
161+
"Classic similarity has been removed",
162+
"https://www.elastic.co/guide/en/elasticsearch/reference/master/" +
163+
"#_the_literal_classic_literal_similarity_has_been_removed",
164+
"Custom similarities defined using classic similarity: " + classicSimilarities.toString());
165+
}
166+
return null;
167+
}
168+
137169
static DeprecationIssue nodeLeftDelayedTimeCheck(IndexMetaData indexMetaData) {
138170
String setting = UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey();
139171
String value = indexMetaData.getSettings().get(setting);
@@ -150,7 +182,7 @@ static DeprecationIssue nodeLeftDelayedTimeCheck(IndexMetaData indexMetaData) {
150182
}
151183
return null;
152184
}
153-
185+
154186
static DeprecationIssue shardOnStartupCheck(IndexMetaData indexMetaData) {
155187
String setting = IndexSettings.INDEX_CHECK_ON_STARTUP.getKey();
156188
String value = indexMetaData.getSettings().get(setting);

x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.xpack.core.deprecation.DeprecationInfoAction;
1616
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
1717

18+
import java.io.IOException;
1819
import java.util.List;
1920
import java.util.Locale;
2021

@@ -109,6 +110,53 @@ public void testPercolatorUnmappedFieldsAsStringCheck() {
109110
assertTrue(noIssues.isEmpty());
110111
}
111112

113+
public void testClassicSimilarityMappingCheck() throws IOException {
114+
String mappingJson = "{\n" +
115+
" \"properties\": {\n" +
116+
" \"default_field\": {\n" +
117+
" \"type\": \"text\"\n" +
118+
" },\n" +
119+
" \"classic_sim_field\": {\n" +
120+
" \"type\": \"text\",\n" +
121+
" \"similarity\": \"classic\"\n" +
122+
" }\n" +
123+
" }\n" +
124+
"}";
125+
IndexMetaData index = IndexMetaData.builder(randomAlphaOfLengthBetween(5,10))
126+
.settings(settings(
127+
VersionUtils.randomVersionBetween(random(), Version.V_6_0_0, VersionUtils.getPreviousVersion(Version.CURRENT))))
128+
.numberOfShards(randomIntBetween(1,100))
129+
.numberOfReplicas(randomIntBetween(1, 100))
130+
.putMapping("_doc", mappingJson)
131+
.build();
132+
DeprecationIssue expected = new DeprecationIssue(DeprecationIssue.Level.WARNING,
133+
"Classic similarity has been removed",
134+
"https://www.elastic.co/guide/en/elasticsearch/reference/master/" +
135+
"#_the_literal_classic_literal_similarity_has_been_removed",
136+
"Fields which use classic similarity: [[type: _doc, field: classic_sim_field]]");
137+
List<DeprecationIssue> issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(index));
138+
assertEquals(singletonList(expected), issues);
139+
}
140+
141+
public void testClassicSimilaritySettingsCheck() {
142+
IndexMetaData index = IndexMetaData.builder(randomAlphaOfLengthBetween(5, 10))
143+
.settings(settings(
144+
VersionUtils.randomVersionBetween(random(), Version.V_6_0_0, VersionUtils.getPreviousVersion(Version.CURRENT)))
145+
.put("index.similarity.my_classic_similarity.type", "classic")
146+
.put("index.similarity.my_okay_similarity.type", "BM25"))
147+
.numberOfShards(randomIntBetween(1, 100))
148+
.numberOfReplicas(randomIntBetween(1, 100))
149+
.build();
150+
151+
DeprecationIssue expected = new DeprecationIssue(DeprecationIssue.Level.WARNING,
152+
"Classic similarity has been removed",
153+
"https://www.elastic.co/guide/en/elasticsearch/reference/master/" +
154+
"#_the_literal_classic_literal_similarity_has_been_removed",
155+
"Custom similarities defined using classic similarity: [my_classic_similarity]");
156+
List<DeprecationIssue> issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(index));
157+
assertEquals(singletonList(expected), issues);
158+
}
159+
112160
public void testNodeLeftDelayedTimeCheck() {
113161
String negativeTimeValue = "-" + randomPositiveTimeValue();
114162
String indexName = randomAlphaOfLengthBetween(0, 10);

0 commit comments

Comments
 (0)