Allow dynamic templates with match_mapping_type but no path_match or match.

As long as we have some selector we're good.

Closes #3814
This commit is contained in:
Boaz Leskes 2013-10-01 17:03:31 +02:00
parent f91fd3b9ed
commit f747704249
2 changed files with 47 additions and 8 deletions

View File

@ -78,8 +78,8 @@ public class DynamicTemplate {
} }
} }
if (match == null && pathMatch == null) { if (match == null && pathMatch == null && matchMappingType == null) {
throw new MapperParsingException("template must have match or path_match set"); throw new MapperParsingException("template must have match, path_match or match_mapping_type set");
} }
if (mapping == null) { if (mapping == null) {
throw new MapperParsingException("template must have mapping set"); throw new MapperParsingException("template must have mapping set");
@ -202,17 +202,28 @@ public class DynamicTemplate {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) {
if (o == null || getClass() != o.getClass()) return false; return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DynamicTemplate that = (DynamicTemplate) o; DynamicTemplate that = (DynamicTemplate) o;
// check if same matching, if so, replace the mapping // check if same matching, if so, replace the mapping
if (match != null ? !match.equals(that.match) : that.match != null) return false; if (match != null ? !match.equals(that.match) : that.match != null) {
if (matchMappingType != null ? !matchMappingType.equals(that.matchMappingType) : that.matchMappingType != null)
return false; 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; return true;
} }

View File

@ -22,9 +22,13 @@ package org.elasticsearch.index.mapper.dynamictemplate.simple;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.bytes.BytesArray; 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.DocumentMapper;
import org.elasticsearch.index.mapper.FieldMappers; import org.elasticsearch.index.mapper.FieldMappers;
import org.elasticsearch.index.mapper.MapperTestUtils; import org.elasticsearch.index.mapper.MapperTestUtils;
import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath; import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath;
@ -37,6 +41,30 @@ import static org.hamcrest.Matchers.equalTo;
*/ */
public class SimpleDynamicTemplatesTests { 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 @Test
public void testSimple() throws Exception { public void testSimple() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/simple/test-mapping.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/simple/test-mapping.json");