Skip to content

Commit

Permalink
Allow dynamic templates with match_mapping_type but no path_match or …
Browse files Browse the repository at this point in the history
…match.

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

Closes #3814
  • Loading branch information
bleskes committed Oct 1, 2013
1 parent f91fd3b commit f747704
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
Expand Up @@ -78,8 +78,8 @@ public static DynamicTemplate parse(String name, Map<String, Object> conf) throw
}
}

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");
Expand Down Expand Up @@ -202,17 +202,28 @@ private List processList(List list, String name, String dynamicType) {

@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 (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;
}
if (matchType != that.matchType) {
return false;
}
if (unmatch != null ? !unmatch.equals(that.unmatch) : that.unmatch != null) {
return false;
}

return true;
}
Expand Down
Expand Up @@ -22,9 +22,13 @@
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;
Expand All @@ -37,6 +41,30 @@
*/
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");
Expand Down

0 comments on commit f747704

Please sign in to comment.