diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java index f10a4c1784d..f3248859dbb 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java @@ -597,11 +597,22 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll { newMapper = true; BuilderContext builderContext = new BuilderContext(context.path()); if (token == XContentParser.Token.VALUE_STRING) { - String text = context.parser().text(); - // check if it fits one of the date formats boolean resolved = false; - // a safe check since "1" gets parsed as well - if (context.root().dateDetection()) { + + // do a quick test to see if its fits a dynamic template, if so, use it. + // we need to do it here so we can handle things like attachment templates, where calling + // text (to see if its a date) causes the binary value to be cleared + if (!resolved) { + Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, "string"); + if (builder != null) { + mapper = builder.build(builderContext); + resolved = true; + } + } + + if (!resolved && context.root().dateDetection()) { + String text = context.parser().text(); + // a safe check since "1" gets parsed as well if (text.contains(":") || text.contains("-") || text.contains("/")) { for (FormatDateTimeFormatter dateTimeFormatter : context.root().dynamicDateTimeFormatters()) { try { @@ -620,6 +631,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll { } } if (!resolved && context.root().numericDetection()) { + String text = context.parser().text(); try { Long.parseLong(text); Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, "long"); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/object/RootObjectMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/object/RootObjectMapper.java index 1f8509cae3d..44a68f67727 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/object/RootObjectMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/object/RootObjectMapper.java @@ -212,7 +212,7 @@ public class RootObjectMapper extends ObjectMapper { if (typeParser == null) { throw new MapperParsingException("failed to find type parsed [" + mappingType + "] for [" + name + "]"); } - return typeParser.parse(name, dynamicTemplate.mappingForName(name, mappingType), parserContext); + return typeParser.parse(name, dynamicTemplate.mappingForName(name, dynamicType), parserContext); } public DynamicTemplate findTemplate(ContentPath path, String name, String dynamicType) {