Skip to content

Commit

Permalink
Restore version dependent logic from CompletionFieldMapper (#119102)
Browse files Browse the repository at this point in the history
This has been removed previously with #113011 but needs to be added back
for v7 read-only compatibility.
  • Loading branch information
cbuescher authored Dec 20, 2024
1 parent ef6372a commit bb0c34e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> ALLOWED_CONTENT_FIELD_NAMES = Set.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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]."
);
}

Expand Down

0 comments on commit bb0c34e

Please sign in to comment.