3838import org .elasticsearch .common .xcontent .XContentParser ;
3939
4040import java .io .IOException ;
41+ import java .util .ArrayList ;
42+ import java .util .Collections ;
43+ import java .util .List ;
4144import java .util .Map ;
4245import java .util .Objects ;
4346import java .util .Set ;
4447
4548public class IndexTemplateMetaData extends AbstractDiffable <IndexTemplateMetaData > {
4649
50+ public static final Version V_5_1_0 = Version .fromId (5010099 );
51+
4752 public static final IndexTemplateMetaData PROTO = IndexTemplateMetaData .builder ("" ).build ();
4853
4954 private final String name ;
@@ -56,7 +61,7 @@ public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaDat
5661 * <pre><code>
5762 * PUT /_template/my_template
5863 * {
59- * "template ": "my_index-*",
64+ * "index_patterns ": [ "my_index-*"] ,
6065 * "mappings": { ... },
6166 * "version": 1
6267 * }
@@ -70,7 +75,7 @@ public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaDat
7075 @ Nullable
7176 private final Integer version ;
7277
73- private final String template ;
78+ private final List < String > patterns ;
7479
7580 private final Settings settings ;
7681
@@ -82,14 +87,14 @@ public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaDat
8287 private final ImmutableOpenMap <String , IndexMetaData .Custom > customs ;
8388
8489 public IndexTemplateMetaData (String name , int order , Integer version ,
85- String template , Settings settings ,
90+ List < String > patterns , Settings settings ,
8691 ImmutableOpenMap <String , CompressedXContent > mappings ,
8792 ImmutableOpenMap <String , AliasMetaData > aliases ,
8893 ImmutableOpenMap <String , IndexMetaData .Custom > customs ) {
8994 this .name = name ;
9095 this .order = order ;
9196 this .version = version ;
92- this .template = template ;
97+ this .patterns = patterns ;
9398 this .settings = settings ;
9499 this .mappings = mappings ;
95100 this .aliases = aliases ;
@@ -122,12 +127,12 @@ public String getName() {
122127 return this .name ;
123128 }
124129
125- public String template () {
126- return this .template ;
130+ public List < String > patterns () {
131+ return this .patterns ;
127132 }
128133
129- public String getTemplate () {
130- return this .template ;
134+ public List < String > getPatterns () {
135+ return this .patterns ;
131136 }
132137
133138 public Settings settings () {
@@ -182,7 +187,7 @@ public boolean equals(Object o) {
182187 if (!mappings .equals (that .mappings )) return false ;
183188 if (!name .equals (that .name )) return false ;
184189 if (!settings .equals (that .settings )) return false ;
185- if (!template .equals (that .template )) return false ;
190+ if (!patterns .equals (that .patterns )) return false ;
186191
187192 return Objects .equals (version , that .version );
188193 }
@@ -192,7 +197,7 @@ public int hashCode() {
192197 int result = name .hashCode ();
193198 result = 31 * result + order ;
194199 result = 31 * result + Objects .hashCode (version );
195- result = 31 * result + template .hashCode ();
200+ result = 31 * result + patterns .hashCode ();
196201 result = 31 * result + settings .hashCode ();
197202 result = 31 * result + mappings .hashCode ();
198203 return result ;
@@ -202,7 +207,11 @@ public int hashCode() {
202207 public IndexTemplateMetaData readFrom (StreamInput in ) throws IOException {
203208 Builder builder = new Builder (in .readString ());
204209 builder .order (in .readInt ());
205- builder .template (in .readString ());
210+ if (in .getVersion ().onOrAfter (V_5_1_0 )) {
211+ builder .patterns (in .readList (StreamInput ::readString ));
212+ } else {
213+ builder .patterns (Collections .singletonList (in .readString ()));
214+ }
206215 builder .settings (Settings .readSettingsFromStream (in ));
207216 int mappingsSize = in .readVInt ();
208217 for (int i = 0 ; i < mappingsSize ; i ++) {
@@ -229,7 +238,11 @@ public IndexTemplateMetaData readFrom(StreamInput in) throws IOException {
229238 public void writeTo (StreamOutput out ) throws IOException {
230239 out .writeString (name );
231240 out .writeInt (order );
232- out .writeString (template );
241+ if (out .getVersion ().onOrAfter (V_5_1_0 )) {
242+ out .writeStringList (patterns );
243+ } else {
244+ out .writeString (patterns .size () > 0 ? patterns .get (0 ) : "" );
245+ }
233246 Settings .writeSettingsToStream (settings , out );
234247 out .writeVInt (mappings .size ());
235248 for (ObjectObjectCursor <String , CompressedXContent > cursor : mappings ) {
@@ -252,7 +265,7 @@ public void writeTo(StreamOutput out) throws IOException {
252265
253266 public static class Builder {
254267
255- private static final Set <String > VALID_FIELDS = Sets .newHashSet ("template" , "order" , "mappings" , "settings" );
268+ private static final Set <String > VALID_FIELDS = Sets .newHashSet ("template" , "order" , "mappings" , "settings" , "index_patterns" );
256269 static {
257270 VALID_FIELDS .addAll (IndexMetaData .customPrototypes .keySet ());
258271 }
@@ -263,7 +276,7 @@ public static class Builder {
263276
264277 private Integer version ;
265278
266- private String template ;
279+ private List < String > indexPatterns ;
267280
268281 private Settings settings = Settings .Builder .EMPTY_SETTINGS ;
269282
@@ -284,7 +297,7 @@ public Builder(IndexTemplateMetaData indexTemplateMetaData) {
284297 this .name = indexTemplateMetaData .name ();
285298 order (indexTemplateMetaData .order ());
286299 version (indexTemplateMetaData .version ());
287- template (indexTemplateMetaData .template ());
300+ patterns (indexTemplateMetaData .patterns ());
288301 settings (indexTemplateMetaData .settings ());
289302
290303 mappings = ImmutableOpenMap .builder (indexTemplateMetaData .mappings ());
@@ -302,14 +315,11 @@ public Builder version(Integer version) {
302315 return this ;
303316 }
304317
305- public Builder template ( String template ) {
306- this .template = template ;
318+ public Builder patterns ( List < String > indexPatterns ) {
319+ this .indexPatterns = indexPatterns ;
307320 return this ;
308321 }
309322
310- public String template () {
311- return template ;
312- }
313323
314324 public Builder settings (Settings .Builder settings ) {
315325 this .settings = settings .build ();
@@ -361,7 +371,8 @@ public IndexMetaData.Custom getCustom(String type) {
361371 }
362372
363373 public IndexTemplateMetaData build () {
364- return new IndexTemplateMetaData (name , order , version , template , settings , mappings .build (), aliases .build (), customs .build ());
374+ return new IndexTemplateMetaData (name , order , version , indexPatterns , settings , mappings .build (),
375+ aliases .build (), customs .build ());
365376 }
366377
367378 @ SuppressWarnings ("unchecked" )
@@ -373,7 +384,7 @@ public static void toXContent(IndexTemplateMetaData indexTemplateMetaData, XCont
373384 if (indexTemplateMetaData .version () != null ) {
374385 builder .field ("version" , indexTemplateMetaData .version ());
375386 }
376- builder .field ("template " , indexTemplateMetaData .template ());
387+ builder .field ("index_patterns " , indexTemplateMetaData .patterns ());
377388
378389 builder .startObject ("settings" );
379390 indexTemplateMetaData .settings ().toXContent (builder , params );
@@ -478,10 +489,17 @@ public static IndexTemplateMetaData fromXContent(XContentParser parser, String t
478489 }
479490 }
480491 }
492+ } else if ("index_patterns" .equals (currentFieldName )) {
493+ List <String > index_patterns = new ArrayList <>();
494+ while ((token = parser .nextToken ()) != XContentParser .Token .END_ARRAY ) {
495+ index_patterns .add (parser .text ());
496+ }
497+ builder .patterns (index_patterns );
481498 }
482499 } else if (token .isValue ()) {
500+ // This is for roll-forward bwc with (#21009)
483501 if ("template" .equals (currentFieldName )) {
484- builder .template ( parser .text ());
502+ builder .patterns ( Collections . singletonList ( parser .text () ));
485503 } else if ("order" .equals (currentFieldName )) {
486504 builder .order (parser .intValue ());
487505 } else if ("version" .equals (currentFieldName )) {
0 commit comments