Skip to content

Commit 0219a21

Browse files
a2linnik9000
authored andcommitted
Allows multiple patterns to be specified for index templates (#21009)
* 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 5c4392e commit 0219a21

34 files changed

+528
-188
lines changed

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

Lines changed: 44 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;
@@ -32,6 +33,8 @@
3233
import org.elasticsearch.common.collect.MapBuilder;
3334
import org.elasticsearch.common.io.stream.StreamInput;
3435
import org.elasticsearch.common.io.stream.StreamOutput;
36+
import org.elasticsearch.common.logging.DeprecationLogger;
37+
import org.elasticsearch.common.logging.Loggers;
3538
import org.elasticsearch.common.settings.Settings;
3639
import org.elasticsearch.common.xcontent.XContentBuilder;
3740
import org.elasticsearch.common.xcontent.XContentFactory;
@@ -41,10 +44,13 @@
4144
import org.elasticsearch.common.xcontent.support.XContentMapValues;
4245

4346
import java.io.IOException;
47+
import java.util.Collections;
4448
import java.util.HashMap;
4549
import java.util.HashSet;
50+
import java.util.List;
4651
import java.util.Map;
4752
import java.util.Set;
53+
import java.util.stream.Collectors;
4854

4955
import static org.elasticsearch.action.ValidateActions.addValidationError;
5056
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
@@ -56,11 +62,15 @@
5662
*/
5763
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest {
5864

65+
private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(Loggers.getLogger(PutIndexTemplateRequest.class));
66+
67+
public static final Version V_5_1_0 = Version.fromId(5010099);
68+
5969
private String name;
6070

6171
private String cause = "";
6272

63-
private String template;
73+
private List<String> indexPatterns;
6474

6575
private int order;
6676

@@ -92,8 +102,8 @@ public ActionRequestValidationException validate() {
92102
if (name == null) {
93103
validationException = addValidationError("name is missing", validationException);
94104
}
95-
if (template == null) {
96-
validationException = addValidationError("template is missing", validationException);
105+
if (indexPatterns == null || indexPatterns.size() == 0) {
106+
validationException = addValidationError("pattern is missing", validationException);
97107
}
98108
return validationException;
99109
}
@@ -113,13 +123,13 @@ public String name() {
113123
return this.name;
114124
}
115125

116-
public PutIndexTemplateRequest template(String template) {
117-
this.template = template;
126+
public PutIndexTemplateRequest patterns(List<String> indexPatterns) {
127+
this.indexPatterns = indexPatterns;
118128
return this;
119129
}
120130

121-
public String template() {
122-
return this.template;
131+
public List<String> patterns() {
132+
return this.indexPatterns;
123133
}
124134

125135
public PutIndexTemplateRequest order(int order) {
@@ -286,7 +296,20 @@ public PutIndexTemplateRequest source(Map templateSource) {
286296
for (Map.Entry<String, Object> entry : source.entrySet()) {
287297
String name = entry.getKey();
288298
if (name.equals("template")) {
289-
template(entry.getValue().toString());
299+
// This is needed to allow for bwc (beats, logstash) with pre-5.0 templates (#21009)
300+
if(entry.getValue() instanceof String) {
301+
DEPRECATION_LOGGER.deprecated("Deprecated field [template] used, replaced by [index_patterns]");
302+
patterns(Collections.singletonList((String) entry.getValue()));
303+
}
304+
} else if (name.equals("index_patterns")) {
305+
if(entry.getValue() instanceof String) {
306+
patterns(Collections.singletonList((String) entry.getValue()));
307+
} else if (entry.getValue() instanceof List) {
308+
List<String> elements = ((List<?>) entry.getValue()).stream().map(Object::toString).collect(Collectors.toList());
309+
patterns(elements);
310+
} else {
311+
throw new IllegalArgumentException("Malformed [template] value, should be a string or a list of strings");
312+
}
290313
} else if (name.equals("order")) {
291314
order(XContentMapValues.nodeIntegerValue(entry.getValue(), order()));
292315
} else if ("version".equals(name)) {
@@ -295,7 +318,7 @@ public PutIndexTemplateRequest source(Map templateSource) {
295318
}
296319
version((Integer)entry.getValue());
297320
} else if (name.equals("settings")) {
298-
if (!(entry.getValue() instanceof Map)) {
321+
if ((entry.getValue() instanceof Map) == false) {
299322
throw new IllegalArgumentException("Malformed [settings] section, should include an inner object");
300323
}
301324
settings((Map<String, Object>) entry.getValue());
@@ -436,7 +459,7 @@ public PutIndexTemplateRequest alias(Alias alias) {
436459

437460
@Override
438461
public String[] indices() {
439-
return new String[]{template};
462+
return indexPatterns.toArray(new String[indexPatterns.size()]);
440463
}
441464

442465
@Override
@@ -449,7 +472,12 @@ public void readFrom(StreamInput in) throws IOException {
449472
super.readFrom(in);
450473
cause = in.readString();
451474
name = in.readString();
452-
template = in.readString();
475+
476+
if (in.getVersion().onOrAfter(V_5_1_0)) {
477+
indexPatterns = in.readList(StreamInput::readString);
478+
} else {
479+
indexPatterns = Collections.singletonList(in.readString());
480+
}
453481
order = in.readInt();
454482
create = in.readBoolean();
455483
settings = readSettingsFromStream(in);
@@ -475,7 +503,11 @@ public void writeTo(StreamOutput out) throws IOException {
475503
super.writeTo(out);
476504
out.writeString(cause);
477505
out.writeString(name);
478-
out.writeString(template);
506+
if (out.getVersion().onOrAfter(V_5_1_0)) {
507+
out.writeStringList(indexPatterns);
508+
} else {
509+
out.writeString(indexPatterns.size() > 0 ? indexPatterns.get(0) : "");
510+
}
479511
out.writeInt(order);
480512
out.writeBoolean(create);
481513
writeSettingsToStream(settings, out);

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

Lines changed: 15 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
@@ -39,10 +41,20 @@ public PutIndexTemplateRequestBuilder(ElasticsearchClient client, PutIndexTempla
3941
}
4042

4143
/**
42-
* Sets the template match expression that will be used to match on indices created.
44+
* Sets the match expression that will be used to match on indices created.
45+
*
46+
* @deprecated Replaced by {@link #setPatterns(List)}
47+
*/
48+
@Deprecated
49+
public PutIndexTemplateRequestBuilder setTemplate(String indexPattern) {
50+
return setPatterns(Collections.singletonList(indexPattern));
51+
}
52+
53+
/**
54+
* Sets the match expression that will be used to match on indices created.
4355
*/
44-
public PutIndexTemplateRequestBuilder setTemplate(String template) {
45-
request.template(template);
56+
public PutIndexTemplateRequestBuilder setPatterns(List<String> indexPatterns) {
57+
request.patterns(indexPatterns);
4658
return this;
4759
}
4860

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
@@ -395,7 +395,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
395395
IndexTemplateMetaData templateMetaData = cursor.value;
396396
builder.startObject(templateMetaData.name());
397397

398-
builder.field("template", templateMetaData.template());
398+
builder.field("index_patterns", templateMetaData.patterns());
399399
builder.field("order", templateMetaData.order());
400400

401401
builder.startObject("settings");

0 commit comments

Comments
 (0)