From bb0c34e7c3d5b73febcae29491fd030361c3d6ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Fri, 20 Dec 2024 23:31:22 +0100 Subject: [PATCH] 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. --- .../index/mapper/CompletionFieldMapper.java | 26 ++++++++++++++-- .../mapper/CompletionFieldMapperTests.java | 31 ++++++++++++++++--- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java index 00c2a872010c7..f0c679d4f4994 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java @@ -18,9 +18,11 @@ import org.apache.lucene.search.suggest.document.RegexCompletionQuery; import org.apache.lucene.search.suggest.document.SuggestField; import org.elasticsearch.common.ParsingException; +import org.elasticsearch.common.logging.DeprecationCategory; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.util.Maps; import org.elasticsearch.index.IndexVersion; +import org.elasticsearch.index.IndexVersions; import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.query.SearchExecutionContext; @@ -207,11 +209,29 @@ public CompletionFieldMapper build(MapperBuilderContext context) { private void checkCompletionContextsLimit() { if (this.contexts.getValue() != null && this.contexts.getValue().size() > COMPLETION_CONTEXTS_LIMIT) { - throw new IllegalArgumentException( - "Limit of completion field contexts [" + COMPLETION_CONTEXTS_LIMIT + "] has been exceeded" - ); + if (indexVersionCreated.onOrAfter(IndexVersions.V_8_0_0)) { + throw new IllegalArgumentException( + "Limit of completion field contexts [" + COMPLETION_CONTEXTS_LIMIT + "] has been exceeded" + ); + } else { + deprecationLogger.warn( + DeprecationCategory.MAPPINGS, + "excessive_completion_contexts", + "You have defined more than [" + + COMPLETION_CONTEXTS_LIMIT + + "] completion contexts" + + " in the mapping for field [" + + leafName() + + "]. " + + "The maximum allowed number of completion contexts in a mapping will be limited to " + + "[" + + COMPLETION_CONTEXTS_LIMIT + + "] starting in version [8.0]." + ); + } } } + } public static final Set ALLOWED_CONTENT_FIELD_NAMES = Set.of( diff --git a/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java index 134d21ba475b7..d5d9d84673a0e 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java @@ -34,6 +34,7 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.index.IndexSettings; +import org.elasticsearch.index.IndexVersions; import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.IndexAnalyzers; import org.elasticsearch.index.analysis.NamedAnalyzer; @@ -60,6 +61,7 @@ import java.util.Set; import java.util.function.Function; +import static org.elasticsearch.index.mapper.CompletionFieldMapper.COMPLETION_CONTEXTS_LIMIT; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; @@ -757,7 +759,7 @@ public void testLimitOfContextMappings() throws Throwable { .startObject("suggest") .field("type", "completion") .startArray("contexts"); - for (int i = 0; i < CompletionFieldMapper.COMPLETION_CONTEXTS_LIMIT + 1; i++) { + for (int i = 0; i < COMPLETION_CONTEXTS_LIMIT + 1; i++) { mappingBuilder.startObject(); mappingBuilder.field("name", Integer.toString(i)); mappingBuilder.field("type", "category"); @@ -769,7 +771,7 @@ public void testLimitOfContextMappings() throws Throwable { MapperParsingException e = expectThrows(MapperParsingException.class, () -> createDocumentMapper(fieldMapping(b -> { b.field("type", "completion"); b.startArray("contexts"); - for (int i = 0; i < CompletionFieldMapper.COMPLETION_CONTEXTS_LIMIT + 1; i++) { + for (int i = 0; i < COMPLETION_CONTEXTS_LIMIT + 1; i++) { b.startObject(); b.field("name", Integer.toString(i)); b.field("type", "category"); @@ -779,8 +781,29 @@ public void testLimitOfContextMappings() throws Throwable { }))); assertTrue( e.getMessage(), - e.getMessage() - .contains("Limit of completion field contexts [" + CompletionFieldMapper.COMPLETION_CONTEXTS_LIMIT + "] has been exceeded") + e.getMessage().contains("Limit of completion field contexts [" + COMPLETION_CONTEXTS_LIMIT + "] has been exceeded") + ); + + // test pre-8 deprecation warnings + createDocumentMapper(IndexVersions.V_7_0_0, fieldMapping(b -> { + b.field("type", "completion"); + b.startArray("contexts"); + for (int i = 0; i < COMPLETION_CONTEXTS_LIMIT + 1; i++) { + b.startObject(); + b.field("name", Integer.toString(i)); + b.field("type", "category"); + b.endObject(); + } + b.endArray(); + })); + assertCriticalWarnings( + "You have defined more than [" + + COMPLETION_CONTEXTS_LIMIT + + "] completion contexts" + + " in the mapping for field [field]. The maximum allowed number of completion contexts in a mapping will be limited to " + + "[" + + COMPLETION_CONTEXTS_LIMIT + + "] starting in version [8.0]." ); }