Skip to content

Commit bb0c34e

Browse files
authored
Restore version dependent logic from CompletionFieldMapper (#119102)
This has been removed previously with #113011 but needs to be added back for v7 read-only compatibility.
1 parent ef6372a commit bb0c34e

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
import org.apache.lucene.search.suggest.document.RegexCompletionQuery;
1919
import org.apache.lucene.search.suggest.document.SuggestField;
2020
import org.elasticsearch.common.ParsingException;
21+
import org.elasticsearch.common.logging.DeprecationCategory;
2122
import org.elasticsearch.common.unit.Fuzziness;
2223
import org.elasticsearch.common.util.Maps;
2324
import org.elasticsearch.index.IndexVersion;
25+
import org.elasticsearch.index.IndexVersions;
2426
import org.elasticsearch.index.analysis.AnalyzerScope;
2527
import org.elasticsearch.index.analysis.NamedAnalyzer;
2628
import org.elasticsearch.index.query.SearchExecutionContext;
@@ -207,11 +209,29 @@ public CompletionFieldMapper build(MapperBuilderContext context) {
207209

208210
private void checkCompletionContextsLimit() {
209211
if (this.contexts.getValue() != null && this.contexts.getValue().size() > COMPLETION_CONTEXTS_LIMIT) {
210-
throw new IllegalArgumentException(
211-
"Limit of completion field contexts [" + COMPLETION_CONTEXTS_LIMIT + "] has been exceeded"
212-
);
212+
if (indexVersionCreated.onOrAfter(IndexVersions.V_8_0_0)) {
213+
throw new IllegalArgumentException(
214+
"Limit of completion field contexts [" + COMPLETION_CONTEXTS_LIMIT + "] has been exceeded"
215+
);
216+
} else {
217+
deprecationLogger.warn(
218+
DeprecationCategory.MAPPINGS,
219+
"excessive_completion_contexts",
220+
"You have defined more than ["
221+
+ COMPLETION_CONTEXTS_LIMIT
222+
+ "] completion contexts"
223+
+ " in the mapping for field ["
224+
+ leafName()
225+
+ "]. "
226+
+ "The maximum allowed number of completion contexts in a mapping will be limited to "
227+
+ "["
228+
+ COMPLETION_CONTEXTS_LIMIT
229+
+ "] starting in version [8.0]."
230+
);
231+
}
213232
}
214233
}
234+
215235
}
216236

217237
public static final Set<String> ALLOWED_CONTENT_FIELD_NAMES = Set.of(

server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.common.util.BigArrays;
3535
import org.elasticsearch.core.CheckedConsumer;
3636
import org.elasticsearch.index.IndexSettings;
37+
import org.elasticsearch.index.IndexVersions;
3738
import org.elasticsearch.index.analysis.AnalyzerScope;
3839
import org.elasticsearch.index.analysis.IndexAnalyzers;
3940
import org.elasticsearch.index.analysis.NamedAnalyzer;
@@ -60,6 +61,7 @@
6061
import java.util.Set;
6162
import java.util.function.Function;
6263

64+
import static org.elasticsearch.index.mapper.CompletionFieldMapper.COMPLETION_CONTEXTS_LIMIT;
6365
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
6466
import static org.hamcrest.Matchers.containsInAnyOrder;
6567
import static org.hamcrest.Matchers.containsString;
@@ -757,7 +759,7 @@ public void testLimitOfContextMappings() throws Throwable {
757759
.startObject("suggest")
758760
.field("type", "completion")
759761
.startArray("contexts");
760-
for (int i = 0; i < CompletionFieldMapper.COMPLETION_CONTEXTS_LIMIT + 1; i++) {
762+
for (int i = 0; i < COMPLETION_CONTEXTS_LIMIT + 1; i++) {
761763
mappingBuilder.startObject();
762764
mappingBuilder.field("name", Integer.toString(i));
763765
mappingBuilder.field("type", "category");
@@ -769,7 +771,7 @@ public void testLimitOfContextMappings() throws Throwable {
769771
MapperParsingException e = expectThrows(MapperParsingException.class, () -> createDocumentMapper(fieldMapping(b -> {
770772
b.field("type", "completion");
771773
b.startArray("contexts");
772-
for (int i = 0; i < CompletionFieldMapper.COMPLETION_CONTEXTS_LIMIT + 1; i++) {
774+
for (int i = 0; i < COMPLETION_CONTEXTS_LIMIT + 1; i++) {
773775
b.startObject();
774776
b.field("name", Integer.toString(i));
775777
b.field("type", "category");
@@ -779,8 +781,29 @@ public void testLimitOfContextMappings() throws Throwable {
779781
})));
780782
assertTrue(
781783
e.getMessage(),
782-
e.getMessage()
783-
.contains("Limit of completion field contexts [" + CompletionFieldMapper.COMPLETION_CONTEXTS_LIMIT + "] has been exceeded")
784+
e.getMessage().contains("Limit of completion field contexts [" + COMPLETION_CONTEXTS_LIMIT + "] has been exceeded")
785+
);
786+
787+
// test pre-8 deprecation warnings
788+
createDocumentMapper(IndexVersions.V_7_0_0, fieldMapping(b -> {
789+
b.field("type", "completion");
790+
b.startArray("contexts");
791+
for (int i = 0; i < COMPLETION_CONTEXTS_LIMIT + 1; i++) {
792+
b.startObject();
793+
b.field("name", Integer.toString(i));
794+
b.field("type", "category");
795+
b.endObject();
796+
}
797+
b.endArray();
798+
}));
799+
assertCriticalWarnings(
800+
"You have defined more than ["
801+
+ COMPLETION_CONTEXTS_LIMIT
802+
+ "] completion contexts"
803+
+ " in the mapping for field [field]. The maximum allowed number of completion contexts in a mapping will be limited to "
804+
+ "["
805+
+ COMPLETION_CONTEXTS_LIMIT
806+
+ "] starting in version [8.0]."
784807
);
785808
}
786809

0 commit comments

Comments
 (0)