Skip to content

Commit

Permalink
Allows multiple patterns to be specified for index templates (#21009)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
a2lin authored and nik9000 committed Nov 10, 2016
1 parent 5c4392e commit 0219a21
Show file tree
Hide file tree
Showing 34 changed files with 528 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.admin.indices.alias.Alias;
Expand All @@ -32,6 +33,8 @@
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
Expand All @@ -41,10 +44,13 @@
import org.elasticsearch.common.xcontent.support.XContentMapValues;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

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

private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(Loggers.getLogger(PutIndexTemplateRequest.class));

public static final Version V_5_1_0 = Version.fromId(5010099);

private String name;

private String cause = "";

private String template;
private List<String> indexPatterns;

private int order;

Expand Down Expand Up @@ -92,8 +102,8 @@ public ActionRequestValidationException validate() {
if (name == null) {
validationException = addValidationError("name is missing", validationException);
}
if (template == null) {
validationException = addValidationError("template is missing", validationException);
if (indexPatterns == null || indexPatterns.size() == 0) {
validationException = addValidationError("pattern is missing", validationException);
}
return validationException;
}
Expand All @@ -113,13 +123,13 @@ public String name() {
return this.name;
}

public PutIndexTemplateRequest template(String template) {
this.template = template;
public PutIndexTemplateRequest patterns(List<String> indexPatterns) {
this.indexPatterns = indexPatterns;
return this;
}

public String template() {
return this.template;
public List<String> patterns() {
return this.indexPatterns;
}

public PutIndexTemplateRequest order(int order) {
Expand Down Expand Up @@ -286,7 +296,20 @@ public PutIndexTemplateRequest source(Map templateSource) {
for (Map.Entry<String, Object> entry : source.entrySet()) {
String name = entry.getKey();
if (name.equals("template")) {
template(entry.getValue().toString());
// This is needed to allow for bwc (beats, logstash) with pre-5.0 templates (#21009)
if(entry.getValue() instanceof String) {
DEPRECATION_LOGGER.deprecated("Deprecated field [template] used, replaced by [index_patterns]");
patterns(Collections.singletonList((String) entry.getValue()));
}
} else if (name.equals("index_patterns")) {
if(entry.getValue() instanceof String) {
patterns(Collections.singletonList((String) entry.getValue()));
} else if (entry.getValue() instanceof List) {
List<String> elements = ((List<?>) entry.getValue()).stream().map(Object::toString).collect(Collectors.toList());
patterns(elements);
} else {
throw new IllegalArgumentException("Malformed [template] value, should be a string or a list of strings");
}
} else if (name.equals("order")) {
order(XContentMapValues.nodeIntegerValue(entry.getValue(), order()));
} else if ("version".equals(name)) {
Expand All @@ -295,7 +318,7 @@ public PutIndexTemplateRequest source(Map templateSource) {
}
version((Integer)entry.getValue());
} else if (name.equals("settings")) {
if (!(entry.getValue() instanceof Map)) {
if ((entry.getValue() instanceof Map) == false) {
throw new IllegalArgumentException("Malformed [settings] section, should include an inner object");
}
settings((Map<String, Object>) entry.getValue());
Expand Down Expand Up @@ -436,7 +459,7 @@ public PutIndexTemplateRequest alias(Alias alias) {

@Override
public String[] indices() {
return new String[]{template};
return indexPatterns.toArray(new String[indexPatterns.size()]);
}

@Override
Expand All @@ -449,7 +472,12 @@ public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
cause = in.readString();
name = in.readString();
template = in.readString();

if (in.getVersion().onOrAfter(V_5_1_0)) {
indexPatterns = in.readList(StreamInput::readString);
} else {
indexPatterns = Collections.singletonList(in.readString());
}
order = in.readInt();
create = in.readBoolean();
settings = readSettingsFromStream(in);
Expand All @@ -475,7 +503,11 @@ public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(cause);
out.writeString(name);
out.writeString(template);
if (out.getVersion().onOrAfter(V_5_1_0)) {
out.writeStringList(indexPatterns);
} else {
out.writeString(indexPatterns.size() > 0 ? indexPatterns.get(0) : "");
}
out.writeInt(order);
out.writeBoolean(create);
writeSettingsToStream(settings, out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.util.Collections;
import java.util.List;
import java.util.Map;

public class PutIndexTemplateRequestBuilder
Expand All @@ -39,10 +41,20 @@ public PutIndexTemplateRequestBuilder(ElasticsearchClient client, PutIndexTempla
}

/**
* Sets the template match expression that will be used to match on indices created.
* Sets the match expression that will be used to match on indices created.
*
* @deprecated Replaced by {@link #setPatterns(List)}
*/
@Deprecated
public PutIndexTemplateRequestBuilder setTemplate(String indexPattern) {
return setPatterns(Collections.singletonList(indexPattern));
}

/**
* Sets the match expression that will be used to match on indices created.
*/
public PutIndexTemplateRequestBuilder setTemplate(String template) {
request.template(template);
public PutIndexTemplateRequestBuilder setPatterns(List<String> indexPatterns) {
request.patterns(indexPatterns);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected void masterOperation(final PutIndexTemplateRequest request, final Clus
templateSettingsBuilder.put(request.settings()).normalizePrefix(IndexMetaData.INDEX_SETTING_PREFIX);
indexScopedSettings.validate(templateSettingsBuilder);
indexTemplateService.putTemplate(new MetaDataIndexTemplateService.PutRequest(cause, request.name())
.template(request.template())
.patterns(request.patterns())
.order(request.order())
.settings(templateSettingsBuilder.build())
.mappings(request.mappings())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
IndexTemplateMetaData templateMetaData = cursor.value;
builder.startObject(templateMetaData.name());

builder.field("template", templateMetaData.template());
builder.field("index_patterns", templateMetaData.patterns());
builder.field("order", templateMetaData.order());

builder.startObject("settings");
Expand Down
Loading

0 comments on commit 0219a21

Please sign in to comment.