|
@@ -186,7 +186,7 @@ public class MetadataIndexTemplateService {
|
|
|
}
|
|
|
|
|
|
CompressedXContent mappings = template.template().mappings();
|
|
|
- String stringMappings = mappings == null ? null : mappings.string();
|
|
|
+ String stringMappings = wrapMappingsIfNecessary(mappings == null ? null : mappings.string(), xContentRegistry);
|
|
|
|
|
|
// We may need to normalize index settings, so do that also
|
|
|
Settings finalSettings = template.template().settings();
|
|
@@ -225,17 +225,6 @@ public class MetadataIndexTemplateService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // Mappings in component templates don't include _doc, so update the mappings to include this single type
|
|
|
- if (stringMappings != null) {
|
|
|
- Map<String, Object> parsedMappings = MapperService.parseMapping(xContentRegistry, stringMappings);
|
|
|
- if (parsedMappings.size() > 0) {
|
|
|
- stringMappings = Strings.toString(XContentFactory.jsonBuilder()
|
|
|
- .startObject()
|
|
|
- .field(MapperService.SINGLE_MAPPING_NAME, parsedMappings)
|
|
|
- .endObject());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
final Template finalTemplate = new Template(finalSettings,
|
|
|
stringMappings == null ? null : new CompressedXContent(stringMappings), template.template().aliases());
|
|
|
final ComponentTemplate finalComponentTemplate = new ComponentTemplate(finalTemplate, template.version(), template.metadata());
|
|
@@ -279,6 +268,35 @@ public class MetadataIndexTemplateService {
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
+ @Nullable
|
|
|
+ private static String wrapMappingsIfNecessary(@Nullable String mappings, NamedXContentRegistry xContentRegistry) throws Exception {
|
|
|
+ // Mappings in templates don't have to include _doc, so update
|
|
|
+ // the mappings to include this single type if necessary
|
|
|
+
|
|
|
+ String stringMappings = mappings;
|
|
|
+ if (stringMappings != null) {
|
|
|
+ Map<String, Object> parsedMappings = MapperService.parseMapping(xContentRegistry, stringMappings);
|
|
|
+ if (parsedMappings.size() > 0) {
|
|
|
+ if (parsedMappings.size() == 1) {
|
|
|
+ final String keyName = parsedMappings.keySet().iterator().next();
|
|
|
+ // Check if it's already wrapped in `_doc`, only rewrap if needed
|
|
|
+ if (MapperService.SINGLE_MAPPING_NAME.equals(keyName) == false) {
|
|
|
+ stringMappings = Strings.toString(XContentFactory.jsonBuilder()
|
|
|
+ .startObject()
|
|
|
+ .field(MapperService.SINGLE_MAPPING_NAME, parsedMappings)
|
|
|
+ .endObject());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ stringMappings = Strings.toString(XContentFactory.jsonBuilder()
|
|
|
+ .startObject()
|
|
|
+ .field(MapperService.SINGLE_MAPPING_NAME, parsedMappings)
|
|
|
+ .endObject());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return stringMappings;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Remove the given component template from the cluster state. The component template name
|
|
|
* supports simple regex wildcards for removing multiple component templates at a time.
|
|
@@ -462,18 +480,8 @@ public class MetadataIndexTemplateService {
|
|
|
// If an inner template was specified, its mappings may need to be
|
|
|
// adjusted (to add _doc) and it should be validated
|
|
|
CompressedXContent mappings = innerTemplate.mappings();
|
|
|
- String stringMappings = mappings == null ? null : mappings.string();
|
|
|
+ String stringMappings = wrapMappingsIfNecessary(mappings == null ? null : mappings.string(), xContentRegistry);
|
|
|
|
|
|
- // Mappings in index templates don't include _doc, so update the mappings to include this single type
|
|
|
- if (stringMappings != null) {
|
|
|
- Map<String, Object> parsedMappings = MapperService.parseMapping(xContentRegistry, stringMappings);
|
|
|
- if (parsedMappings.size() > 0) {
|
|
|
- stringMappings = Strings.toString(XContentFactory.jsonBuilder()
|
|
|
- .startObject()
|
|
|
- .field(MapperService.SINGLE_MAPPING_NAME, parsedMappings)
|
|
|
- .endObject());
|
|
|
- }
|
|
|
- }
|
|
|
final Template finalTemplate = new Template(finalSettings,
|
|
|
stringMappings == null ? null : new CompressedXContent(stringMappings), innerTemplate.aliases());
|
|
|
finalIndexTemplate = new ComposableIndexTemplate(template.indexPatterns(), finalTemplate, template.composedOf(),
|