From 8d533e8a990e2503c5eb9718e5703d2ff360b473 Mon Sep 17 00:00:00 2001 From: kimchy Date: Thu, 14 Oct 2010 12:56:50 +0200 Subject: [PATCH] dynamic tempaltes are now named (change to the mapping definition) to allow for simpler merging --- .../mapper/xcontent/DynamicTemplate.java | 39 +++++++++++-------- .../mapper/xcontent/RootObjectMapper.java | 30 ++++++++++---- .../genericstore/test-mapping.json | 8 ++-- .../dynamictemplate/simple/test-mapping.json | 26 +++++++------ 4 files changed, 65 insertions(+), 38 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/DynamicTemplate.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/DynamicTemplate.java index cfab8f98f84..4969dce0412 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/DynamicTemplate.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/DynamicTemplate.java @@ -47,6 +47,23 @@ public class DynamicTemplate { } } + public static DynamicTemplate parse(String name, Map 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 mapping = (Map) 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 conf; private final String match; @@ -59,22 +76,8 @@ public class DynamicTemplate { private final Map mapping; - public static DynamicTemplate parse(Map 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 mapping = (Map) 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 conf, String match, String unmatch, String matchMappingType, MatchType matchType, Map mapping) { + public DynamicTemplate(String name, Map conf, String match, String unmatch, String matchMappingType, MatchType matchType, Map mapping) { + this.name = name; this.conf = conf; this.match = match; this.unmatch = unmatch; @@ -83,6 +86,10 @@ public class DynamicTemplate { this.mapping = mapping; } + public String name() { + return this.name; + } + public Map conf() { return this.conf; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/RootObjectMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/RootObjectMapper.java index 106ca991252..3c8103da740 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/RootObjectMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/RootObjectMapper.java @@ -22,6 +22,7 @@ package org.elasticsearch.index.mapper.xcontent; import org.elasticsearch.common.collect.Lists; import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.index.mapper.MapperParsingException; import java.io.IOException; import java.util.Arrays; @@ -72,15 +73,21 @@ public class RootObjectMapper extends ObjectMapper { if (fieldName.equals("dynamic_templates")) { // "dynamic_templates" : [ // { - // "match" : "*_test", - // "match_mapping_type" : "string", - // "mapping" : { "type" : "string", "store" : "yes" } + // "template_1" : { + // "match" : "*_test", + // "match_mapping_type" : "string", + // "mapping" : { "type" : "string", "store" : "yes" } + // } // } // ] List tmplNodes = (List) fieldNode; for (Object tmplNode : tmplNodes) { Map tmpl = (Map) 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 entry = tmpl.entrySet().iterator().next(); + ((Builder) builder).add(DynamicTemplate.parse(entry.getKey(), (Map) entry.getValue())); } } } @@ -118,11 +125,15 @@ public class RootObjectMapper extends ObjectMapper { // merge them List mergedTemplates = Lists.newArrayList(Arrays.asList(this.dynamicTemplates)); for (DynamicTemplate template : mergeWithObject.dynamicTemplates) { - int index = mergedTemplates.indexOf(template); - if (index == -1) { + boolean replaced = false; + 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); - } else { - mergedTemplates.set(index, template); } } this.dynamicTemplates = mergedTemplates.toArray(new DynamicTemplate[mergedTemplates.size()]); @@ -133,7 +144,10 @@ public class RootObjectMapper extends ObjectMapper { if (dynamicTemplates != null && dynamicTemplates.length > 0) { builder.startArray("dynamic_templates"); for (DynamicTemplate dynamicTemplate : dynamicTemplates) { + builder.startObject(); + builder.field(dynamicTemplate.name()); builder.map(dynamicTemplate.conf()); + builder.endObject(); } builder.endArray(); } diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/dynamictemplate/genericstore/test-mapping.json b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/dynamictemplate/genericstore/test-mapping.json index 3e08bde6e19..94a3d1a7cdf 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/dynamictemplate/genericstore/test-mapping.json +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/dynamictemplate/genericstore/test-mapping.json @@ -2,9 +2,11 @@ "person" : { "dynamic_templates" : [ { - "match" : "*", - "mapping" : { - "store" : "yes" + "template_1" : { + "match" : "*", + "mapping" : { + "store" : "yes" + } } } ] diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/dynamictemplate/simple/test-mapping.json b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/dynamictemplate/simple/test-mapping.json index 492ab0cec31..9b44f28b57f 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/dynamictemplate/simple/test-mapping.json +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/dynamictemplate/simple/test-mapping.json @@ -2,21 +2,25 @@ "person" : { "dynamic_templates" : [ { - "match" : "multi*", - "mapping" : { - "type" : "multi_field", - "fields" : { - "{name}" : {"type": "{dynamic_type}", "index" : "analyzed", "store" : "yes"}, - "org" : {"type": "{dynamic_type}", "index" : "not_analyzed", "store" : "yes"} + "tempalte_1" : { + "match" : "multi*", + "mapping" : { + "type" : "multi_field", + "fields" : { + "{name}" : {"type": "{dynamic_type}", "index" : "analyzed", "store" : "yes"}, + "org" : {"type": "{dynamic_type}", "index" : "not_analyzed", "store" : "yes"} + } } } }, { - "match" : "*", - "match_mapping_type" : "string", - "mapping" : { - "type" : "string", - "index" : "not_analyzed" + "template_2" : { + "match" : "*", + "match_mapping_type" : "string", + "mapping" : { + "type" : "string", + "index" : "not_analyzed" + } } } ]