dynamic tempaltes are now named (change to the mapping definition) to allow for simpler merging

This commit is contained in:
kimchy 2010-10-14 12:56:50 +02:00
parent ad01f19db8
commit 8d533e8a99
4 changed files with 65 additions and 38 deletions

View File

@ -47,6 +47,23 @@ public class DynamicTemplate {
} }
} }
public static DynamicTemplate parse(String name, Map<String, Object> conf) throws MapperParsingException {
if (!conf.containsKey("match")) {
throw new MapperParsingException("template must have match set");
}
String match = conf.get("match").toString();
String unmatch = conf.containsKey("unmatch") ? conf.get("unmatch").toString() : null;
String matchMappingType = conf.containsKey("match_mapping_type") ? conf.get("match_mapping_type").toString() : null;
if (!conf.containsKey("mapping")) {
throw new MapperParsingException("template must have mapping set");
}
Map<String, Object> mapping = (Map<String, Object>) conf.get("mapping");
String matchType = conf.containsKey("match_pattern") ? conf.get("match_pattern").toString() : "simple";
return new DynamicTemplate(name, conf, match, unmatch, matchMappingType, MatchType.fromString(matchType), mapping);
}
private final String name;
private final Map<String, Object> conf; private final Map<String, Object> conf;
private final String match; private final String match;
@ -59,22 +76,8 @@ public class DynamicTemplate {
private final Map<String, Object> mapping; private final Map<String, Object> mapping;
public static DynamicTemplate parse(Map<String, Object> conf) throws MapperParsingException { public DynamicTemplate(String name, Map<String, Object> conf, String match, String unmatch, String matchMappingType, MatchType matchType, Map<String, Object> mapping) {
if (!conf.containsKey("match")) { this.name = name;
throw new MapperParsingException("template must have match set");
}
String match = conf.get("match").toString();
String unmatch = conf.containsKey("unmatch") ? conf.get("unmatch").toString() : null;
String matchMappingType = conf.containsKey("match_mapping_type") ? conf.get("match_mapping_type").toString() : null;
if (!conf.containsKey("mapping")) {
throw new MapperParsingException("template must have mapping set");
}
Map<String, Object> mapping = (Map<String, Object>) conf.get("mapping");
String matchType = conf.containsKey("match_pattern") ? conf.get("match_pattern").toString() : "simple";
return new DynamicTemplate(conf, match, unmatch, matchMappingType, MatchType.fromString(matchType), mapping);
}
public DynamicTemplate(Map<String, Object> conf, String match, String unmatch, String matchMappingType, MatchType matchType, Map<String, Object> mapping) {
this.conf = conf; this.conf = conf;
this.match = match; this.match = match;
this.unmatch = unmatch; this.unmatch = unmatch;
@ -83,6 +86,10 @@ public class DynamicTemplate {
this.mapping = mapping; this.mapping = mapping;
} }
public String name() {
return this.name;
}
public Map<String, Object> conf() { public Map<String, Object> conf() {
return this.conf; return this.conf;
} }

View File

@ -22,6 +22,7 @@ package org.elasticsearch.index.mapper.xcontent;
import org.elasticsearch.common.collect.Lists; import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.joda.FormatDateTimeFormatter;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.MapperParsingException;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
@ -72,15 +73,21 @@ public class RootObjectMapper extends ObjectMapper {
if (fieldName.equals("dynamic_templates")) { if (fieldName.equals("dynamic_templates")) {
// "dynamic_templates" : [ // "dynamic_templates" : [
// { // {
// "match" : "*_test", // "template_1" : {
// "match_mapping_type" : "string", // "match" : "*_test",
// "mapping" : { "type" : "string", "store" : "yes" } // "match_mapping_type" : "string",
// "mapping" : { "type" : "string", "store" : "yes" }
// }
// } // }
// ] // ]
List tmplNodes = (List) fieldNode; List tmplNodes = (List) fieldNode;
for (Object tmplNode : tmplNodes) { for (Object tmplNode : tmplNodes) {
Map<String, Object> tmpl = (Map<String, Object>) tmplNode; Map<String, Object> tmpl = (Map<String, Object>) tmplNode;
((Builder) builder).add(DynamicTemplate.parse(tmpl)); if (tmpl.size() != 1) {
throw new MapperParsingException("A dynamic template must be defined with a name");
}
Map.Entry<String, Object> entry = tmpl.entrySet().iterator().next();
((Builder) builder).add(DynamicTemplate.parse(entry.getKey(), (Map<String, Object>) entry.getValue()));
} }
} }
} }
@ -118,11 +125,15 @@ public class RootObjectMapper extends ObjectMapper {
// merge them // merge them
List<DynamicTemplate> mergedTemplates = Lists.newArrayList(Arrays.asList(this.dynamicTemplates)); List<DynamicTemplate> mergedTemplates = Lists.newArrayList(Arrays.asList(this.dynamicTemplates));
for (DynamicTemplate template : mergeWithObject.dynamicTemplates) { for (DynamicTemplate template : mergeWithObject.dynamicTemplates) {
int index = mergedTemplates.indexOf(template); boolean replaced = false;
if (index == -1) { for (int i = 0; i < mergedTemplates.size(); i++) {
if (mergedTemplates.get(i).name().equals(template.name())) {
mergedTemplates.set(i, template);
replaced = true;
}
}
if (!replaced) {
mergedTemplates.add(template); mergedTemplates.add(template);
} else {
mergedTemplates.set(index, template);
} }
} }
this.dynamicTemplates = mergedTemplates.toArray(new DynamicTemplate[mergedTemplates.size()]); this.dynamicTemplates = mergedTemplates.toArray(new DynamicTemplate[mergedTemplates.size()]);
@ -133,7 +144,10 @@ public class RootObjectMapper extends ObjectMapper {
if (dynamicTemplates != null && dynamicTemplates.length > 0) { if (dynamicTemplates != null && dynamicTemplates.length > 0) {
builder.startArray("dynamic_templates"); builder.startArray("dynamic_templates");
for (DynamicTemplate dynamicTemplate : dynamicTemplates) { for (DynamicTemplate dynamicTemplate : dynamicTemplates) {
builder.startObject();
builder.field(dynamicTemplate.name());
builder.map(dynamicTemplate.conf()); builder.map(dynamicTemplate.conf());
builder.endObject();
} }
builder.endArray(); builder.endArray();
} }

View File

@ -2,9 +2,11 @@
"person" : { "person" : {
"dynamic_templates" : [ "dynamic_templates" : [
{ {
"match" : "*", "template_1" : {
"mapping" : { "match" : "*",
"store" : "yes" "mapping" : {
"store" : "yes"
}
} }
} }
] ]

View File

@ -2,21 +2,25 @@
"person" : { "person" : {
"dynamic_templates" : [ "dynamic_templates" : [
{ {
"match" : "multi*", "tempalte_1" : {
"mapping" : { "match" : "multi*",
"type" : "multi_field", "mapping" : {
"fields" : { "type" : "multi_field",
"{name}" : {"type": "{dynamic_type}", "index" : "analyzed", "store" : "yes"}, "fields" : {
"org" : {"type": "{dynamic_type}", "index" : "not_analyzed", "store" : "yes"} "{name}" : {"type": "{dynamic_type}", "index" : "analyzed", "store" : "yes"},
"org" : {"type": "{dynamic_type}", "index" : "not_analyzed", "store" : "yes"}
}
} }
} }
}, },
{ {
"match" : "*", "template_2" : {
"match_mapping_type" : "string", "match" : "*",
"mapping" : { "match_mapping_type" : "string",
"type" : "string", "mapping" : {
"index" : "not_analyzed" "type" : "string",
"index" : "not_analyzed"
}
} }
} }
] ]