@@ -264,58 +264,64 @@ public ClusterState execute(ClusterState currentState) throws Exception {
264264 customs .put (entry .getKey (), entry .getValue ());
265265 }
266266
267- // apply templates, merging the mappings into the request mapping if exists
268- for (IndexTemplateMetaData template : templates ) {
269- templateNames .add (template .getName ());
270- for (ObjectObjectCursor <String , CompressedXContent > cursor : template .mappings ()) {
271- String mappingString = cursor .value .string ();
272- if (mappings .containsKey (cursor .key )) {
273- XContentHelper .mergeDefaults (mappings .get (cursor .key ),
267+ final Index shrinkFromIndex = request .shrinkFrom ();
268+
269+ if (shrinkFromIndex == null ) {
270+ // apply templates, merging the mappings into the request mapping if exists
271+ for (IndexTemplateMetaData template : templates ) {
272+ templateNames .add (template .getName ());
273+ for (ObjectObjectCursor <String , CompressedXContent > cursor : template .mappings ()) {
274+ String mappingString = cursor .value .string ();
275+ if (mappings .containsKey (cursor .key )) {
276+ XContentHelper .mergeDefaults (mappings .get (cursor .key ),
274277 MapperService .parseMapping (xContentRegistry , mappingString ));
275- } else {
276- mappings .put (cursor .key ,
277- MapperService .parseMapping (xContentRegistry , mappingString ));
278- }
279- }
280- // handle custom
281- for (ObjectObjectCursor <String , Custom > cursor : template .customs ()) {
282- String type = cursor .key ;
283- IndexMetaData .Custom custom = cursor .value ;
284- IndexMetaData .Custom existing = customs .get (type );
285- if (existing == null ) {
286- customs .put (type , custom );
287- } else {
288- IndexMetaData .Custom merged = existing .mergeWith (custom );
289- customs .put (type , merged );
290- }
291- }
292- //handle aliases
293- for (ObjectObjectCursor <String , AliasMetaData > cursor : template .aliases ()) {
294- AliasMetaData aliasMetaData = cursor .value ;
295- //if an alias with same name came with the create index request itself,
296- // ignore this one taken from the index template
297- if (request .aliases ().contains (new Alias (aliasMetaData .alias ()))) {
298- continue ;
278+ } else {
279+ mappings .put (cursor .key ,
280+ MapperService .parseMapping (xContentRegistry , mappingString ));
281+ }
299282 }
300- //if an alias with same name was already processed, ignore this one
301- if (templatesAliases .containsKey (cursor .key )) {
302- continue ;
283+ // handle custom
284+ for (ObjectObjectCursor <String , Custom > cursor : template .customs ()) {
285+ String type = cursor .key ;
286+ IndexMetaData .Custom custom = cursor .value ;
287+ IndexMetaData .Custom existing = customs .get (type );
288+ if (existing == null ) {
289+ customs .put (type , custom );
290+ } else {
291+ IndexMetaData .Custom merged = existing .mergeWith (custom );
292+ customs .put (type , merged );
293+ }
303294 }
304-
305- //Allow templatesAliases to be templated by replacing a token with the name of the index that we are applying it to
306- if (aliasMetaData .alias ().contains ("{index}" )) {
307- String templatedAlias = aliasMetaData .alias ().replace ("{index}" , request .index ());
308- aliasMetaData = AliasMetaData .newAliasMetaData (aliasMetaData , templatedAlias );
295+ //handle aliases
296+ for (ObjectObjectCursor <String , AliasMetaData > cursor : template .aliases ()) {
297+ AliasMetaData aliasMetaData = cursor .value ;
298+ //if an alias with same name came with the create index request itself,
299+ // ignore this one taken from the index template
300+ if (request .aliases ().contains (new Alias (aliasMetaData .alias ()))) {
301+ continue ;
302+ }
303+ //if an alias with same name was already processed, ignore this one
304+ if (templatesAliases .containsKey (cursor .key )) {
305+ continue ;
306+ }
307+
308+ //Allow templatesAliases to be templated by replacing a token with the name of the index that we are applying it to
309+ if (aliasMetaData .alias ().contains ("{index}" )) {
310+ String templatedAlias = aliasMetaData .alias ().replace ("{index}" , request .index ());
311+ aliasMetaData = AliasMetaData .newAliasMetaData (aliasMetaData , templatedAlias );
312+ }
313+
314+ aliasValidator .validateAliasMetaData (aliasMetaData , request .index (), currentState .metaData ());
315+ templatesAliases .put (aliasMetaData .alias (), aliasMetaData );
309316 }
310-
311- aliasValidator .validateAliasMetaData (aliasMetaData , request .index (), currentState .metaData ());
312- templatesAliases .put (aliasMetaData .alias (), aliasMetaData );
313317 }
314318 }
315319 Settings .Builder indexSettingsBuilder = Settings .builder ();
316- // apply templates, here, in reverse order, since first ones are better matching
317- for (int i = templates .size () - 1 ; i >= 0 ; i --) {
318- indexSettingsBuilder .put (templates .get (i ).settings ());
320+ if (shrinkFromIndex == null ) {
321+ // apply templates, here, in reverse order, since first ones are better matching
322+ for (int i = templates .size () - 1 ; i >= 0 ; i --) {
323+ indexSettingsBuilder .put (templates .get (i ).settings ());
324+ }
319325 }
320326 // now, put the request settings, so they override templates
321327 indexSettingsBuilder .put (request .settings ());
@@ -340,7 +346,6 @@ public ClusterState execute(ClusterState currentState) throws Exception {
340346 }
341347 indexSettingsBuilder .put (IndexMetaData .SETTING_INDEX_PROVIDED_NAME , request .getProvidedName ());
342348 indexSettingsBuilder .put (SETTING_INDEX_UUID , UUIDs .randomBase64UUID ());
343- final Index shrinkFromIndex = request .shrinkFrom ();
344349 final IndexMetaData .Builder tmpImdBuilder = IndexMetaData .builder (request .index ());
345350
346351 final int routingNumShards ;
0 commit comments