@@ -188,7 +188,7 @@ ClusterState addComponentTemplate(final ClusterState currentState, final boolean
188
188
}
189
189
190
190
CompressedXContent mappings = template .template ().mappings ();
191
- String stringMappings = mappings == null ? null : mappings .string ();
191
+ String stringMappings = wrapMappingsIfNecessary ( mappings == null ? null : mappings .string (), xContentRegistry );
192
192
193
193
// We may need to normalize index settings, so do that also
194
194
Settings finalSettings = template .template ().settings ();
@@ -227,17 +227,6 @@ ClusterState addComponentTemplate(final ClusterState currentState, final boolean
227
227
}
228
228
}
229
229
230
- // Mappings in component templates don't include _doc, so update the mappings to include this single type
231
- if (stringMappings != null ) {
232
- Map <String , Object > parsedMappings = MapperService .parseMapping (xContentRegistry , stringMappings );
233
- if (parsedMappings .size () > 0 ) {
234
- stringMappings = Strings .toString (XContentFactory .jsonBuilder ()
235
- .startObject ()
236
- .field (MapperService .SINGLE_MAPPING_NAME , parsedMappings )
237
- .endObject ());
238
- }
239
- }
240
-
241
230
final Template finalTemplate = new Template (finalSettings ,
242
231
stringMappings == null ? null : new CompressedXContent (stringMappings ), template .template ().aliases ());
243
232
final ComponentTemplate finalComponentTemplate = new ComponentTemplate (finalTemplate , template .version (), template .metadata ());
@@ -281,6 +270,35 @@ ClusterState addComponentTemplate(final ClusterState currentState, final boolean
281
270
.build ();
282
271
}
283
272
273
+ @ Nullable
274
+ private static String wrapMappingsIfNecessary (@ Nullable String mappings , NamedXContentRegistry xContentRegistry ) throws Exception {
275
+ // Mappings in templates don't have to include _doc, so update
276
+ // the mappings to include this single type if necessary
277
+
278
+ String stringMappings = mappings ;
279
+ if (stringMappings != null ) {
280
+ Map <String , Object > parsedMappings = MapperService .parseMapping (xContentRegistry , stringMappings );
281
+ if (parsedMappings .size () > 0 ) {
282
+ if (parsedMappings .size () == 1 ) {
283
+ final String keyName = parsedMappings .keySet ().iterator ().next ();
284
+ // Check if it's already wrapped in `_doc`, only rewrap if needed
285
+ if (MapperService .SINGLE_MAPPING_NAME .equals (keyName ) == false ) {
286
+ stringMappings = Strings .toString (XContentFactory .jsonBuilder ()
287
+ .startObject ()
288
+ .field (MapperService .SINGLE_MAPPING_NAME , parsedMappings )
289
+ .endObject ());
290
+ }
291
+ } else {
292
+ stringMappings = Strings .toString (XContentFactory .jsonBuilder ()
293
+ .startObject ()
294
+ .field (MapperService .SINGLE_MAPPING_NAME , parsedMappings )
295
+ .endObject ());
296
+ }
297
+ }
298
+ }
299
+ return stringMappings ;
300
+ }
301
+
284
302
/**
285
303
* Remove the given component template from the cluster state. The component template name
286
304
* supports simple regex wildcards for removing multiple component templates at a time.
@@ -464,18 +482,8 @@ public ClusterState addIndexTemplateV2(final ClusterState currentState, final bo
464
482
// If an inner template was specified, its mappings may need to be
465
483
// adjusted (to add _doc) and it should be validated
466
484
CompressedXContent mappings = innerTemplate .mappings ();
467
- String stringMappings = mappings == null ? null : mappings .string ();
485
+ String stringMappings = wrapMappingsIfNecessary ( mappings == null ? null : mappings .string (), xContentRegistry );
468
486
469
- // Mappings in index templates don't include _doc, so update the mappings to include this single type
470
- if (stringMappings != null ) {
471
- Map <String , Object > parsedMappings = MapperService .parseMapping (xContentRegistry , stringMappings );
472
- if (parsedMappings .size () > 0 ) {
473
- stringMappings = Strings .toString (XContentFactory .jsonBuilder ()
474
- .startObject ()
475
- .field (MapperService .SINGLE_MAPPING_NAME , parsedMappings )
476
- .endObject ());
477
- }
478
- }
479
487
final Template finalTemplate = new Template (finalSettings ,
480
488
stringMappings == null ? null : new CompressedXContent (stringMappings ), innerTemplate .aliases ());
481
489
finalIndexTemplate = new ComposableIndexTemplate (template .indexPatterns (), finalTemplate , template .composedOf (),
0 commit comments