@@ -263,58 +263,64 @@ public ClusterState execute(ClusterState currentState) throws Exception {
263263 customs .put (entry .getKey (), entry .getValue ());
264264 }
265265
266- // apply templates, merging the mappings into the request mapping if exists
267- for (IndexTemplateMetaData template : templates ) {
268- templateNames .add (template .getName ());
269- for (ObjectObjectCursor <String , CompressedXContent > cursor : template .mappings ()) {
270- String mappingString = cursor .value .string ();
271- if (mappings .containsKey (cursor .key )) {
272- XContentHelper .mergeDefaults (mappings .get (cursor .key ),
266+ final Index shrinkFromIndex = request .shrinkFrom ();
267+
268+ if (shrinkFromIndex == null ) {
269+ // apply templates, merging the mappings into the request mapping if exists
270+ for (IndexTemplateMetaData template : templates ) {
271+ templateNames .add (template .getName ());
272+ for (ObjectObjectCursor <String , CompressedXContent > cursor : template .mappings ()) {
273+ String mappingString = cursor .value .string ();
274+ if (mappings .containsKey (cursor .key )) {
275+ XContentHelper .mergeDefaults (mappings .get (cursor .key ),
273276 MapperService .parseMapping (xContentRegistry , mappingString ));
274- } else {
275- mappings .put (cursor .key ,
276- MapperService .parseMapping (xContentRegistry , mappingString ));
277- }
278- }
279- // handle custom
280- for (ObjectObjectCursor <String , Custom > cursor : template .customs ()) {
281- String type = cursor .key ;
282- IndexMetaData .Custom custom = cursor .value ;
283- IndexMetaData .Custom existing = customs .get (type );
284- if (existing == null ) {
285- customs .put (type , custom );
286- } else {
287- IndexMetaData .Custom merged = existing .mergeWith (custom );
288- customs .put (type , merged );
289- }
290- }
291- //handle aliases
292- for (ObjectObjectCursor <String , AliasMetaData > cursor : template .aliases ()) {
293- AliasMetaData aliasMetaData = cursor .value ;
294- //if an alias with same name came with the create index request itself,
295- // ignore this one taken from the index template
296- if (request .aliases ().contains (new Alias (aliasMetaData .alias ()))) {
297- continue ;
277+ } else {
278+ mappings .put (cursor .key ,
279+ MapperService .parseMapping (xContentRegistry , mappingString ));
280+ }
298281 }
299- //if an alias with same name was already processed, ignore this one
300- if (templatesAliases .containsKey (cursor .key )) {
301- continue ;
282+ // handle custom
283+ for (ObjectObjectCursor <String , Custom > cursor : template .customs ()) {
284+ String type = cursor .key ;
285+ IndexMetaData .Custom custom = cursor .value ;
286+ IndexMetaData .Custom existing = customs .get (type );
287+ if (existing == null ) {
288+ customs .put (type , custom );
289+ } else {
290+ IndexMetaData .Custom merged = existing .mergeWith (custom );
291+ customs .put (type , merged );
292+ }
302293 }
303-
304- //Allow templatesAliases to be templated by replacing a token with the name of the index that we are applying it to
305- if (aliasMetaData .alias ().contains ("{index}" )) {
306- String templatedAlias = aliasMetaData .alias ().replace ("{index}" , request .index ());
307- aliasMetaData = AliasMetaData .newAliasMetaData (aliasMetaData , templatedAlias );
294+ //handle aliases
295+ for (ObjectObjectCursor <String , AliasMetaData > cursor : template .aliases ()) {
296+ AliasMetaData aliasMetaData = cursor .value ;
297+ //if an alias with same name came with the create index request itself,
298+ // ignore this one taken from the index template
299+ if (request .aliases ().contains (new Alias (aliasMetaData .alias ()))) {
300+ continue ;
301+ }
302+ //if an alias with same name was already processed, ignore this one
303+ if (templatesAliases .containsKey (cursor .key )) {
304+ continue ;
305+ }
306+
307+ //Allow templatesAliases to be templated by replacing a token with the name of the index that we are applying it to
308+ if (aliasMetaData .alias ().contains ("{index}" )) {
309+ String templatedAlias = aliasMetaData .alias ().replace ("{index}" , request .index ());
310+ aliasMetaData = AliasMetaData .newAliasMetaData (aliasMetaData , templatedAlias );
311+ }
312+
313+ aliasValidator .validateAliasMetaData (aliasMetaData , request .index (), currentState .metaData ());
314+ templatesAliases .put (aliasMetaData .alias (), aliasMetaData );
308315 }
309-
310- aliasValidator .validateAliasMetaData (aliasMetaData , request .index (), currentState .metaData ());
311- templatesAliases .put (aliasMetaData .alias (), aliasMetaData );
312316 }
313317 }
314318 Settings .Builder indexSettingsBuilder = Settings .builder ();
315- // apply templates, here, in reverse order, since first ones are better matching
316- for (int i = templates .size () - 1 ; i >= 0 ; i --) {
317- indexSettingsBuilder .put (templates .get (i ).settings ());
319+ if (shrinkFromIndex == null ) {
320+ // apply templates, here, in reverse order, since first ones are better matching
321+ for (int i = templates .size () - 1 ; i >= 0 ; i --) {
322+ indexSettingsBuilder .put (templates .get (i ).settings ());
323+ }
318324 }
319325 // now, put the request settings, so they override templates
320326 indexSettingsBuilder .put (request .settings ());
@@ -339,20 +345,26 @@ public ClusterState execute(ClusterState currentState) throws Exception {
339345 }
340346 indexSettingsBuilder .put (IndexMetaData .SETTING_INDEX_PROVIDED_NAME , request .getProvidedName ());
341347 indexSettingsBuilder .put (SETTING_INDEX_UUID , UUIDs .randomBase64UUID ());
342- final Index shrinkFromIndex = request .shrinkFrom ();
343- int routingNumShards = IndexMetaData .INDEX_NUMBER_OF_SHARDS_SETTING .get (indexSettingsBuilder .build ());;
344- if (shrinkFromIndex != null ) {
345- prepareShrinkIndexSettings (currentState , mappings .keySet (), indexSettingsBuilder , shrinkFromIndex ,
346- request .index ());
347- IndexMetaData sourceMetaData = currentState .metaData ().getIndexSafe (shrinkFromIndex );
348+ final IndexMetaData .Builder tmpImdBuilder = IndexMetaData .builder (request .index ());
349+
350+ final int routingNumShards ;
351+ if (shrinkFromIndex == null ) {
352+ routingNumShards = IndexMetaData .INDEX_NUMBER_OF_SHARDS_SETTING .get (indexSettingsBuilder .build ());
353+ } else {
354+ final IndexMetaData sourceMetaData = currentState .metaData ().getIndexSafe (shrinkFromIndex );
348355 routingNumShards = sourceMetaData .getRoutingNumShards ();
349356 }
357+ tmpImdBuilder .setRoutingNumShards (routingNumShards );
358+
359+ if (shrinkFromIndex != null ) {
360+ prepareShrinkIndexSettings (
361+ currentState , mappings .keySet (), indexSettingsBuilder , shrinkFromIndex , request .index ());
362+ }
363+ final Settings actualIndexSettings = indexSettingsBuilder .build ();
364+ tmpImdBuilder .settings (actualIndexSettings );
350365
351- Settings actualIndexSettings = indexSettingsBuilder .build ();
352- IndexMetaData .Builder tmpImdBuilder = IndexMetaData .builder (request .index ())
353- .setRoutingNumShards (routingNumShards );
354366 // Set up everything, now locally create the index to see that things are ok, and apply
355- final IndexMetaData tmpImd = tmpImdBuilder .settings ( actualIndexSettings ). build ();
367+ final IndexMetaData tmpImd = tmpImdBuilder .build ();
356368 ActiveShardCount waitForActiveShards = request .waitForActiveShards ();
357369 if (waitForActiveShards == ActiveShardCount .DEFAULT ) {
358370 waitForActiveShards = tmpImd .getWaitForActiveShards ();
@@ -399,6 +411,11 @@ public ClusterState execute(ClusterState currentState) throws Exception {
399411 final IndexMetaData .Builder indexMetaDataBuilder = IndexMetaData .builder (request .index ())
400412 .settings (actualIndexSettings )
401413 .setRoutingNumShards (routingNumShards );
414+
415+ for (int shardId = 0 ; shardId < tmpImd .getNumberOfShards (); shardId ++) {
416+ indexMetaDataBuilder .primaryTerm (shardId , tmpImd .primaryTerm (shardId ));
417+ }
418+
402419 for (MappingMetaData mappingMd : mappingsMetaData .values ()) {
403420 indexMetaDataBuilder .putMapping (mappingMd );
404421 }
0 commit comments