diff --git a/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java b/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java index 82eaf2db84d..b44353dda87 100644 --- a/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java @@ -520,16 +520,16 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll { objectMapper = mappers.get(currentFieldName); if (objectMapper == null) { newMapper = true; + // remove the current field name from path, since template search and the object builder add it as well... + context.path().remove(); Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, "object"); if (builder == null) { builder = MapperBuilders.object(currentFieldName).enabled(true).dynamic(dynamic).pathType(pathType); } - // remove the current field name from path, since the object builder adds it as well... - context.path().remove(); BuilderContext builderContext = new BuilderContext(context.indexSettings(), context.path()); objectMapper = builder.build(builderContext); putMapper(objectMapper); - // now re add it + // ...now re add it context.path().add(currentFieldName); context.setMappingsModified(); } diff --git a/src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/PathMatchDynamicTempalteTests.java b/src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/PathMatchDynamicTemplateTests.java similarity index 87% rename from src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/PathMatchDynamicTempalteTests.java rename to src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/PathMatchDynamicTemplateTests.java index fb9f25f968e..b4583809ec2 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/PathMatchDynamicTempalteTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/PathMatchDynamicTemplateTests.java @@ -22,6 +22,7 @@ package org.elasticsearch.test.unit.index.mapper.dynamictemplate.pathmatch; import org.apache.lucene.document.Document; import org.apache.lucene.document.Fieldable; import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.index.field.data.strings.StringFieldDataType; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.FieldMappers; import org.elasticsearch.test.unit.index.mapper.MapperTests; @@ -31,11 +32,12 @@ import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath; import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; /** * */ -public class PathMatchDynamicTempalteTests { +public class PathMatchDynamicTemplateTests { @Test public void testSimple() throws Exception { @@ -68,5 +70,11 @@ public class PathMatchDynamicTempalteTests { fieldMappers = docMapper.mappers().fullName("obj1.obj2.name"); assertThat(fieldMappers.mappers().size(), equalTo(1)); assertThat(fieldMappers.mapper().stored(), equalTo(false)); + + // verify more complex path_match expressions + + fieldMappers = docMapper.mappers().fullName("obj3.obj4.prop1"); + assertThat(fieldMappers.mappers().size(), equalTo(1)); + assertThat(fieldMappers.mapper().fieldDataType(), instanceOf(StringFieldDataType.class)); } } diff --git a/src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/test-data.json b/src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/test-data.json index 2d3a2f7cc72..818bedd6c8b 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/test-data.json +++ b/src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/test-data.json @@ -6,5 +6,10 @@ "obj2":{ "name":"obj2_level" } + }, + "obj3":{ + "obj4":{ + "prop1":"prop1_value" + } } } \ No newline at end of file diff --git a/src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/test-mapping.json b/src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/test-mapping.json index 3806d844ed8..dce33dadfcc 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/test-mapping.json +++ b/src/test/java/org/elasticsearch/test/unit/index/mapper/dynamictemplate/pathmatch/test-mapping.json @@ -16,6 +16,14 @@ "store":"yes" } } + }, + { + "template_3":{ + "path_match":"*.obj4.*", + "mapping":{ + "type":"string" + } + } } ] }