Skip to content

Commit cc69e06

Browse files
authored
Re-add support for some metadata field parameters (#118825)
We removed support for type, fields, copy_to and boost in metadata field definitions with #116944 but with the move towards supporting N-2 read-only indices we need to add them back. This change reverts previous removal commits and adapts tests to also check we now throw errors for newly created indices.
1 parent 24773e0 commit cc69e06

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
pr: 116944
1+
pr: 118825
22
summary: "Remove support for type, fields, `copy_to` and boost in metadata field definition"
33
area: Mapping
44
type: breaking
55
issues: []
66
breaking:
77
title: "Remove support for type, fields, copy_to and boost in metadata field definition"
88
area: Mapping
9-
details: The type, fields, copy_to and boost parameters are no longer supported in metadata field definition
9+
details: The type, fields, copy_to and boost parameters are no longer supported in metadata field definition starting with version 9.
1010
impact: Users providing type, fields, copy_to or boost as part of metadata field definition should remove them from their mappings.
1111
notable: false

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010
package org.elasticsearch.index.mapper;
1111

1212
import org.elasticsearch.common.Explicit;
13+
import org.elasticsearch.common.logging.DeprecationCategory;
1314
import org.elasticsearch.common.util.Maps;
1415
import org.elasticsearch.common.xcontent.support.XContentMapValues;
16+
import org.elasticsearch.index.IndexVersion;
17+
import org.elasticsearch.index.IndexVersions;
1518
import org.elasticsearch.xcontent.XContentBuilder;
1619

1720
import java.io.IOException;
1821
import java.util.Iterator;
1922
import java.util.Map;
23+
import java.util.Set;
2024
import java.util.function.Function;
2125

2226
/**
@@ -132,6 +136,8 @@ public final MetadataFieldMapper build(MapperBuilderContext context) {
132136
return build();
133137
}
134138

139+
private static final Set<String> UNSUPPORTED_PARAMETERS_8_6_0 = Set.of("type", "fields", "copy_to", "boost");
140+
135141
public final void parseMetadataField(String name, MappingParserContext parserContext, Map<String, Object> fieldNode) {
136142
final Parameter<?>[] params = getParameters();
137143
Map<String, Parameter<?>> paramsMap = Maps.newHashMapWithExpectedSize(params.length);
@@ -144,6 +150,22 @@ public final void parseMetadataField(String name, MappingParserContext parserCon
144150
final Object propNode = entry.getValue();
145151
Parameter<?> parameter = paramsMap.get(propName);
146152
if (parameter == null) {
153+
IndexVersion indexVersionCreated = parserContext.indexVersionCreated();
154+
if (indexVersionCreated.before(IndexVersions.UPGRADE_TO_LUCENE_10_0_0)
155+
&& UNSUPPORTED_PARAMETERS_8_6_0.contains(propName)) {
156+
if (indexVersionCreated.onOrAfter(IndexVersions.V_8_6_0)) {
157+
// silently ignore type, and a few other parameters: sadly we've been doing this for a long time
158+
deprecationLogger.warn(
159+
DeprecationCategory.API,
160+
propName,
161+
"Parameter [{}] has no effect on metadata field [{}] and will be removed in future",
162+
propName,
163+
name
164+
);
165+
}
166+
iterator.remove();
167+
continue;
168+
}
147169
throw new MapperParsingException("unknown parameter [" + propName + "] on metadata field [" + name + "]");
148170
}
149171
parameter.parse(name, parserContext, propNode);

test/framework/src/main/java/org/elasticsearch/index/KnownIndexVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class KnownIndexVersions {
1919
* A sorted list of all known index versions
2020
*/
2121
public static final List<IndexVersion> ALL_VERSIONS = List.copyOf(IndexVersions.getAllVersions());
22+
2223
/**
2324
* A sorted list of all known index versions that can be written to
2425
*/

test/framework/src/main/java/org/elasticsearch/index/mapper/MetadataMapperTestCase.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.common.compress.CompressedXContent;
1313
import org.elasticsearch.core.CheckedConsumer;
1414
import org.elasticsearch.index.IndexVersion;
15+
import org.elasticsearch.index.IndexVersions;
1516
import org.elasticsearch.index.mapper.MapperService.MergeReason;
1617
import org.elasticsearch.test.index.IndexVersionUtils;
1718
import org.elasticsearch.xcontent.XContentBuilder;
@@ -142,4 +143,88 @@ public final void testFixedMetaFieldsAreNotConfigurable() throws IOException {
142143
);
143144
assertEquals("Failed to parse mapping: " + fieldName() + " is not configurable", exception.getMessage());
144145
}
146+
147+
public void testTypeAndFriendsAreAcceptedBefore_8_6_0() throws IOException {
148+
assumeTrue("Metadata field " + fieldName() + " isn't configurable", isConfigurable());
149+
IndexVersion previousVersion = IndexVersionUtils.getPreviousVersion(IndexVersions.V_8_6_0);
150+
// we randomly also pick read-only versions to test that we can still parse the parameters for them
151+
IndexVersion version = IndexVersionUtils.randomVersionBetween(
152+
random(),
153+
IndexVersionUtils.getLowestReadCompatibleVersion(),
154+
previousVersion
155+
);
156+
assumeTrue("Metadata field " + fieldName() + " is not supported on version " + version, isSupportedOn(version));
157+
MapperService mapperService = createMapperService(version, mapping(b -> {}));
158+
// these parameters were previously silently ignored, they will still be ignored in existing indices
159+
String[] unsupportedParameters = new String[] { "fields", "copy_to", "boost", "type" };
160+
for (String param : unsupportedParameters) {
161+
String mappingAsString = "{\n"
162+
+ " \"_doc\" : {\n"
163+
+ " \""
164+
+ fieldName()
165+
+ "\" : {\n"
166+
+ " \""
167+
+ param
168+
+ "\" : \"any\"\n"
169+
+ " }\n"
170+
+ " }\n"
171+
+ "}";
172+
assertNotNull(mapperService.parseMapping("_doc", MergeReason.MAPPING_UPDATE, new CompressedXContent(mappingAsString)));
173+
}
174+
}
175+
176+
public void testTypeAndFriendsAreDeprecatedFrom_8_6_0_TO_9_0_0() throws IOException {
177+
assumeTrue("Metadata field " + fieldName() + " isn't configurable", isConfigurable());
178+
IndexVersion previousVersion = IndexVersionUtils.getPreviousVersion(IndexVersions.UPGRADE_TO_LUCENE_10_0_0);
179+
IndexVersion version = IndexVersionUtils.randomVersionBetween(random(), IndexVersions.V_8_6_0, previousVersion);
180+
assumeTrue("Metadata field " + fieldName() + " is not supported on version " + version, isSupportedOn(version));
181+
MapperService mapperService = createMapperService(version, mapping(b -> {}));
182+
// these parameters were deprecated, they now should throw an error in new indices
183+
String[] unsupportedParameters = new String[] { "fields", "copy_to", "boost", "type" };
184+
for (String param : unsupportedParameters) {
185+
String mappingAsString = "{\n"
186+
+ " \"_doc\" : {\n"
187+
+ " \""
188+
+ fieldName()
189+
+ "\" : {\n"
190+
+ " \""
191+
+ param
192+
+ "\" : \"any\"\n"
193+
+ " }\n"
194+
+ " }\n"
195+
+ "}";
196+
assertNotNull(mapperService.parseMapping("_doc", MergeReason.MAPPING_UPDATE, new CompressedXContent(mappingAsString)));
197+
assertWarnings("Parameter [" + param + "] has no effect on metadata field [" + fieldName() + "] and will be removed in future");
198+
}
199+
}
200+
201+
public void testTypeAndFriendsThrow_After_9_0_0() throws IOException {
202+
assumeTrue("Metadata field " + fieldName() + " isn't configurable", isConfigurable());
203+
IndexVersion version = IndexVersionUtils.randomVersionBetween(
204+
random(),
205+
IndexVersions.UPGRADE_TO_LUCENE_10_0_0,
206+
IndexVersion.current()
207+
);
208+
assumeTrue("Metadata field " + fieldName() + " is not supported on version " + version, isSupportedOn(version));
209+
MapperService mapperService = createMapperService(version, mapping(b -> {}));
210+
// these parameters were previously silently ignored, they are now deprecated in new indices
211+
String[] unsupportedParameters = new String[] { "fields", "copy_to", "boost", "type" };
212+
for (String param : unsupportedParameters) {
213+
String mappingAsString = "{\n"
214+
+ " \"_doc\" : {\n"
215+
+ " \""
216+
+ fieldName()
217+
+ "\" : {\n"
218+
+ " \""
219+
+ param
220+
+ "\" : \"any\"\n"
221+
+ " }\n"
222+
+ " }\n"
223+
+ "}";
224+
expectThrows(
225+
MapperParsingException.class,
226+
() -> mapperService.parseMapping("_doc", MergeReason.MAPPING_UPDATE, new CompressedXContent(mappingAsString))
227+
);
228+
}
229+
}
145230
}

0 commit comments

Comments
 (0)