diff --git a/src/main/java/org/elasticsearch/index/mapper/object/DynamicTemplate.java b/src/main/java/org/elasticsearch/index/mapper/object/DynamicTemplate.java index 87c72235b77..2b948650fc4 100644 --- a/src/main/java/org/elasticsearch/index/mapper/object/DynamicTemplate.java +++ b/src/main/java/org/elasticsearch/index/mapper/object/DynamicTemplate.java @@ -78,8 +78,8 @@ public class DynamicTemplate { } } - if (match == null && pathMatch == null) { - throw new MapperParsingException("template must have match or path_match set"); + if (match == null && pathMatch == null && matchMappingType == null) { + throw new MapperParsingException("template must have match, path_match or match_mapping_type set"); } if (mapping == null) { throw new MapperParsingException("template must have mapping set"); @@ -202,17 +202,28 @@ public class DynamicTemplate { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } DynamicTemplate that = (DynamicTemplate) o; // check if same matching, if so, replace the mapping - if (match != null ? !match.equals(that.match) : that.match != null) return false; - if (matchMappingType != null ? !matchMappingType.equals(that.matchMappingType) : that.matchMappingType != null) + if (match != null ? !match.equals(that.match) : that.match != null) { return false; - if (matchType != that.matchType) return false; - if (unmatch != null ? !unmatch.equals(that.unmatch) : that.unmatch != null) return false; + } + if (matchMappingType != null ? !matchMappingType.equals(that.matchMappingType) : that.matchMappingType != null) { + return false; + } + if (matchType != that.matchType) { + return false; + } + if (unmatch != null ? !unmatch.equals(that.unmatch) : that.unmatch != null) { + return false; + } return true; } diff --git a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/simple/SimpleDynamicTemplatesTests.java b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/simple/SimpleDynamicTemplatesTests.java index fdd183d65a1..f4dce29e02a 100644 --- a/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/simple/SimpleDynamicTemplatesTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/dynamictemplate/simple/SimpleDynamicTemplatesTests.java @@ -22,9 +22,13 @@ package org.elasticsearch.index.mapper.dynamictemplate.simple; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexableField; import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.json.JsonXContent; +import org.elasticsearch.index.mapper.DocumentFieldMappers; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.FieldMappers; import org.elasticsearch.index.mapper.MapperTestUtils; +import org.hamcrest.Matchers; import org.junit.Test; import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath; @@ -37,6 +41,30 @@ import static org.hamcrest.Matchers.equalTo; */ public class SimpleDynamicTemplatesTests { + @Test + public void testMatchTypeOnly() throws Exception { + XContentBuilder builder = JsonXContent.contentBuilder(); + builder.startObject().startObject("person").startArray("dynamic_templates").startObject().startObject("test") + .field("match_mapping_type", "string") + .startObject("mapping").field("index", "no").endObject() + .endObject().endObject().endArray().endObject().endObject(); + DocumentMapper docMapper = MapperTestUtils.newParser().parse(builder.string()); + builder = JsonXContent.contentBuilder(); + builder.startObject().field("_id", "1").field("s", "hello").field("l", 1).endObject(); + docMapper.parse(builder.bytes()); + + DocumentFieldMappers mappers = docMapper.mappers(); + + assertThat(mappers.smartName("s"), Matchers.notNullValue()); + assertThat(mappers.smartName("s").mapper().fieldType().indexed(), equalTo(false)); + + assertThat(mappers.smartName("l"), Matchers.notNullValue()); + assertThat(mappers.smartName("l").mapper().fieldType().indexed(), equalTo(true)); + + + } + + @Test public void testSimple() throws Exception { String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/simple/test-mapping.json");