Skip to content

Commit ca18747

Browse files
committed
Allows for an array of index template patterns to be provided to an
index template, and rename the field from 'template' to 'index_pattern'. Closes #20690
1 parent d77d4fa commit ca18747

File tree

29 files changed

+495
-174
lines changed

29 files changed

+495
-174
lines changed

core/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.elasticsearch.ElasticsearchGenerationException;
2222
import org.elasticsearch.ElasticsearchParseException;
23+
import org.elasticsearch.Version;
2324
import org.elasticsearch.action.ActionRequestValidationException;
2425
import org.elasticsearch.action.IndicesRequest;
2526
import org.elasticsearch.action.admin.indices.alias.Alias;
@@ -41,10 +42,13 @@
4142
import org.elasticsearch.common.xcontent.support.XContentMapValues;
4243

4344
import java.io.IOException;
45+
import java.util.Collections;
4446
import java.util.HashMap;
4547
import java.util.HashSet;
48+
import java.util.List;
4649
import java.util.Map;
4750
import java.util.Set;
51+
import java.util.stream.Collectors;
4852

4953
import static org.elasticsearch.action.ValidateActions.addValidationError;
5054
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
@@ -56,11 +60,13 @@
5660
*/
5761
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest {
5862

63+
public static final Version V_5_1_0 = Version.fromId(5010099);
64+
5965
private String name;
6066

6167
private String cause = "";
6268

63-
private String template;
69+
private List<String> indexPatterns;
6470

6571
private int order;
6672

@@ -92,8 +98,8 @@ public ActionRequestValidationException validate() {
9298
if (name == null) {
9399
validationException = addValidationError("name is missing", validationException);
94100
}
95-
if (template == null) {
96-
validationException = addValidationError("template is missing", validationException);
101+
if (indexPatterns == null || indexPatterns.size() == 0) {
102+
validationException = addValidationError("pattern is missing", validationException);
97103
}
98104
return validationException;
99105
}
@@ -113,13 +119,13 @@ public String name() {
113119
return this.name;
114120
}
115121

116-
public PutIndexTemplateRequest template(String template) {
117-
this.template = template;
122+
public PutIndexTemplateRequest patterns(List<String> indexPatterns) {
123+
this.indexPatterns = indexPatterns;
118124
return this;
119125
}
120126

121-
public String template() {
122-
return this.template;
127+
public List<String> patterns() {
128+
return this.indexPatterns;
123129
}
124130

125131
public PutIndexTemplateRequest order(int order) {
@@ -286,7 +292,19 @@ public PutIndexTemplateRequest source(Map templateSource) {
286292
for (Map.Entry<String, Object> entry : source.entrySet()) {
287293
String name = entry.getKey();
288294
if (name.equals("template")) {
289-
template(entry.getValue().toString());
295+
// This is needed to allow for bwc (beats, logstash) with pre-5.0 templates (#21009)
296+
if(entry.getValue() instanceof String) {
297+
patterns(Collections.singletonList((String) entry.getValue()));
298+
}
299+
} else if (name.equals("index_patterns")) {
300+
if(entry.getValue() instanceof String) {
301+
patterns(Collections.singletonList((String) entry.getValue()));
302+
} else if (entry.getValue() instanceof List) {
303+
List<String> elements = ((List<?>) entry.getValue()).stream().map(Object::toString).collect(Collectors.toList());
304+
patterns(elements);
305+
} else {
306+
throw new IllegalArgumentException("Malformed [template] value, should be a string or a list of strings");
307+
}
290308
} else if (name.equals("order")) {
291309
order(XContentMapValues.nodeIntegerValue(entry.getValue(), order()));
292310
} else if ("version".equals(name)) {
@@ -295,7 +313,7 @@ public PutIndexTemplateRequest source(Map templateSource) {
295313
}
296314
version((Integer)entry.getValue());
297315
} else if (name.equals("settings")) {
298-
if (!(entry.getValue() instanceof Map)) {
316+
if ((entry.getValue() instanceof Map) == false) {
299317
throw new IllegalArgumentException("Malformed [settings] section, should include an inner object");
300318
}
301319
settings((Map<String, Object>) entry.getValue());
@@ -436,7 +454,7 @@ public PutIndexTemplateRequest alias(Alias alias) {
436454

437455
@Override
438456
public String[] indices() {
439-
return new String[]{template};
457+
return indexPatterns.toArray(new String[indexPatterns.size()]);
440458
}
441459

442460
@Override
@@ -449,7 +467,12 @@ public void readFrom(StreamInput in) throws IOException {
449467
super.readFrom(in);
450468
cause = in.readString();
451469
name = in.readString();
452-
template = in.readString();
470+
471+
if (in.getVersion().onOrAfter(V_5_1_0)) {
472+
indexPatterns = in.readList(StreamInput::readString);
473+
} else {
474+
indexPatterns = Collections.singletonList(in.readString());
475+
}
453476
order = in.readInt();
454477
create = in.readBoolean();
455478
settings = readSettingsFromStream(in);
@@ -475,7 +498,11 @@ public void writeTo(StreamOutput out) throws IOException {
475498
super.writeTo(out);
476499
out.writeString(cause);
477500
out.writeString(name);
478-
out.writeString(template);
501+
if (out.getVersion().onOrAfter(V_5_1_0)) {
502+
out.writeStringList(indexPatterns);
503+
} else {
504+
out.writeString(indexPatterns.size() > 0 ? indexPatterns.get(0) : "");
505+
}
479506
out.writeInt(order);
480507
out.writeBoolean(create);
481508
writeSettingsToStream(settings, out);

core/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.elasticsearch.common.settings.Settings;
2626
import org.elasticsearch.common.xcontent.XContentBuilder;
2727

28+
import java.util.Collections;
29+
import java.util.List;
2830
import java.util.Map;
2931

3032
public class PutIndexTemplateRequestBuilder
@@ -38,11 +40,16 @@ public PutIndexTemplateRequestBuilder(ElasticsearchClient client, PutIndexTempla
3840
super(client, action, new PutIndexTemplateRequest(name));
3941
}
4042

43+
@Deprecated
44+
public PutIndexTemplateRequestBuilder setTemplate(String indexPattern) {
45+
return setPatterns(Collections.singletonList(indexPattern));
46+
}
47+
4148
/**
42-
* Sets the template match expression that will be used to match on indices created.
49+
* Sets the match expression that will be used to match on indices created.
4350
*/
44-
public PutIndexTemplateRequestBuilder setTemplate(String template) {
45-
request.template(template);
51+
public PutIndexTemplateRequestBuilder setPatterns(List<String> indexPatterns) {
52+
request.patterns(indexPatterns);
4653
return this;
4754
}
4855

core/src/main/java/org/elasticsearch/action/admin/indices/template/put/TransportPutIndexTemplateAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected void masterOperation(final PutIndexTemplateRequest request, final Clus
7979
templateSettingsBuilder.put(request.settings()).normalizePrefix(IndexMetaData.INDEX_SETTING_PREFIX);
8080
indexScopedSettings.validate(templateSettingsBuilder);
8181
indexTemplateService.putTemplate(new MetaDataIndexTemplateService.PutRequest(cause, request.name())
82-
.template(request.template())
82+
.patterns(request.patterns())
8383
.order(request.order())
8484
.settings(templateSettingsBuilder.build())
8585
.mappings(request.mappings())

core/src/main/java/org/elasticsearch/cluster/ClusterState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
425425
IndexTemplateMetaData templateMetaData = cursor.value;
426426
builder.startObject(templateMetaData.name());
427427

428-
builder.field("template", templateMetaData.template());
428+
builder.field("index_patterns", templateMetaData.patterns());
429429
builder.field("order", templateMetaData.order());
430430

431431
builder.startObject("settings");

core/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetaData.java

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@
3838
import org.elasticsearch.common.xcontent.XContentParser;
3939

4040
import java.io.IOException;
41+
import java.util.ArrayList;
42+
import java.util.Collections;
43+
import java.util.List;
4144
import java.util.Map;
4245
import java.util.Objects;
4346
import java.util.Set;
4447

4548
public 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

Comments
 (0)