Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore version dependent logic from CompletionFieldMapper #119102

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
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.logging.DeprecationLogger;
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 @@ -210,11 +212,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