Skip to content

Commit

Permalink
[Remove] DynamicTemplate deprecations (#1742)
Browse files Browse the repository at this point in the history
This commit removes legacy version checks in DynamicTemplate parsing that are no
longer valid in OpenSearch 2.0.0.

Signed-off-by: Nicholas Walter Knize <[email protected]>
  • Loading branch information
nknize authored Dec 16, 2021
1 parent 5550f8d commit ef44182
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@

package org.opensearch.index.mapper;

import org.opensearch.LegacyESVersion;
import org.opensearch.Version;
import org.opensearch.common.logging.DeprecationLogger;
import org.opensearch.common.regex.Regex;
import org.opensearch.common.xcontent.ToXContentObject;
import org.opensearch.common.xcontent.XContentBuilder;
Expand All @@ -49,8 +46,6 @@

public class DynamicTemplate implements ToXContentObject {

private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(DynamicTemplate.class);

public enum MatchType {
SIMPLE {
@Override
Expand Down Expand Up @@ -183,7 +178,7 @@ public static XContentFieldType fromString(String value) {
public abstract String defaultMappingType();
}

public static DynamicTemplate parse(String name, Map<String, Object> conf, Version indexVersionCreated) throws MapperParsingException {
public static DynamicTemplate parse(String name, Map<String, Object> conf) throws MapperParsingException {
String match = null;
String pathMatch = null;
String unmatch = null;
Expand Down Expand Up @@ -229,20 +224,18 @@ public static DynamicTemplate parse(String name, Map<String, Object> conf, Versi

final MatchType matchType = MatchType.fromString(matchPattern);

if (indexVersionCreated.onOrAfter(LegacyESVersion.V_6_3_0)) {
// Validate that the pattern
for (String regex : new String[] { pathMatch, match, pathUnmatch, unmatch }) {
if (regex == null) {
continue;
}
try {
matchType.matches(regex, "");
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
"Pattern [" + regex + "] of type [" + matchType + "] is invalid. Cannot create dynamic template [" + name + "].",
e
);
}
// Validate the pattern
for (String regex : new String[] { pathMatch, match, pathUnmatch, unmatch }) {
if (regex == null) {
continue;
}
try {
matchType.matches(regex, "");
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
"Pattern [" + regex + "] of type [" + matchType + "] is invalid. Cannot create dynamic template [" + name + "].",
e
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ protected boolean processField(RootObjectMapper.Builder builder, String fieldNam
Map.Entry<String, Object> entry = tmpl.entrySet().iterator().next();
String templateName = entry.getKey();
Map<String, Object> templateParams = (Map<String, Object>) entry.getValue();
DynamicTemplate template = DynamicTemplate.parse(templateName, templateParams, parserContext.indexVersionCreated());
DynamicTemplate template = DynamicTemplate.parse(templateName, templateParams);
if (template != null) {
validateDynamicTemplate(parserContext, template);
templates.add(template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

package org.opensearch.index.mapper;

import org.opensearch.LegacyESVersion;
import org.opensearch.Version;
import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.ToXContent;
import org.opensearch.common.xcontent.XContentBuilder;
Expand All @@ -53,10 +51,7 @@ public void testParseUnknownParam() throws Exception {
templateDef.put("mapping", Collections.singletonMap("store", true));
templateDef.put("random_param", "random_value");

IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> DynamicTemplate.parse("my_template", templateDef, Version.CURRENT.minimumIndexCompatibilityVersion())
);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> DynamicTemplate.parse("my_template", templateDef));
assertEquals("Illegal dynamic template parameter: [random_param]", e.getMessage());
}

Expand All @@ -65,10 +60,7 @@ public void testParseUnknownMatchType() {
templateDef2.put("match_mapping_type", "text");
templateDef2.put("mapping", Collections.singletonMap("store", true));
// if a wrong match type is specified, we ignore the template
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> DynamicTemplate.parse("my_template", templateDef2, Version.CURRENT.minimumIndexCompatibilityVersion())
);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> DynamicTemplate.parse("my_template", templateDef2));
assertEquals(
"No field type matched on [text], possible values are [object, string, long, double, boolean, date, binary]",
e.getMessage()
Expand All @@ -84,7 +76,7 @@ public void testParseInvalidRegex() {
templateDef.put("mapping", Collections.singletonMap("store", true));
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> DynamicTemplate.parse("my_template", templateDef, LegacyESVersion.V_6_3_0)
() -> DynamicTemplate.parse("my_template", templateDef)
);
assertEquals("Pattern [*a] of type [regex] is invalid. Cannot create dynamic template [my_template].", e.getMessage());
}
Expand All @@ -94,15 +86,15 @@ public void testMatchAllTemplate() {
Map<String, Object> templateDef = new HashMap<>();
templateDef.put("match_mapping_type", "*");
templateDef.put("mapping", Collections.singletonMap("store", true));
DynamicTemplate template = DynamicTemplate.parse("my_template", templateDef, Version.CURRENT.minimumIndexCompatibilityVersion());
DynamicTemplate template = DynamicTemplate.parse("my_template", templateDef);
assertTrue(template.match("a.b", "b", randomFrom(XContentFieldType.values())));
}

public void testMatchTypeTemplate() {
Map<String, Object> templateDef = new HashMap<>();
templateDef.put("match_mapping_type", "string");
templateDef.put("mapping", Collections.singletonMap("store", true));
DynamicTemplate template = DynamicTemplate.parse("my_template", templateDef, Version.CURRENT.minimumIndexCompatibilityVersion());
DynamicTemplate template = DynamicTemplate.parse("my_template", templateDef);
assertTrue(template.match("a.b", "b", XContentFieldType.STRING));
assertFalse(template.match("a.b", "b", XContentFieldType.BOOLEAN));
}
Expand All @@ -112,7 +104,7 @@ public void testSerialization() throws Exception {
Map<String, Object> templateDef = new HashMap<>();
templateDef.put("match_mapping_type", "string");
templateDef.put("mapping", Collections.singletonMap("store", true));
DynamicTemplate template = DynamicTemplate.parse("my_template", templateDef, Version.CURRENT.minimumIndexCompatibilityVersion());
DynamicTemplate template = DynamicTemplate.parse("my_template", templateDef);
XContentBuilder builder = JsonXContent.contentBuilder();
template.toXContent(builder, ToXContent.EMPTY_PARAMS);
assertEquals("{\"match_mapping_type\":\"string\",\"mapping\":{\"store\":true}}", Strings.toString(builder));
Expand All @@ -122,7 +114,7 @@ public void testSerialization() throws Exception {
templateDef.put("match", "*name");
templateDef.put("unmatch", "first_name");
templateDef.put("mapping", Collections.singletonMap("store", true));
template = DynamicTemplate.parse("my_template", templateDef, Version.CURRENT.minimumIndexCompatibilityVersion());
template = DynamicTemplate.parse("my_template", templateDef);
builder = JsonXContent.contentBuilder();
template.toXContent(builder, ToXContent.EMPTY_PARAMS);
assertEquals("{\"match\":\"*name\",\"unmatch\":\"first_name\",\"mapping\":{\"store\":true}}", Strings.toString(builder));
Expand All @@ -132,7 +124,7 @@ public void testSerialization() throws Exception {
templateDef.put("path_match", "*name");
templateDef.put("path_unmatch", "first_name");
templateDef.put("mapping", Collections.singletonMap("store", true));
template = DynamicTemplate.parse("my_template", templateDef, Version.CURRENT.minimumIndexCompatibilityVersion());
template = DynamicTemplate.parse("my_template", templateDef);
builder = JsonXContent.contentBuilder();
template.toXContent(builder, ToXContent.EMPTY_PARAMS);
assertEquals("{\"path_match\":\"*name\",\"path_unmatch\":\"first_name\",\"mapping\":{\"store\":true}}", Strings.toString(builder));
Expand All @@ -142,7 +134,7 @@ public void testSerialization() throws Exception {
templateDef.put("match", "^a$");
templateDef.put("match_pattern", "regex");
templateDef.put("mapping", Collections.singletonMap("store", true));
template = DynamicTemplate.parse("my_template", templateDef, Version.CURRENT.minimumIndexCompatibilityVersion());
template = DynamicTemplate.parse("my_template", templateDef);
builder = JsonXContent.contentBuilder();
template.toXContent(builder, ToXContent.EMPTY_PARAMS);
assertEquals("{\"match\":\"^a$\",\"match_pattern\":\"regex\",\"mapping\":{\"store\":true}}", Strings.toString(builder));
Expand Down

0 comments on commit ef44182

Please sign in to comment.