Skip to content

Commit

Permalink
Allows for an array of index template patterns to be provided to an
Browse files Browse the repository at this point in the history
index template, and rename the field from 'template' to 'index_pattern'.

Closes #20690
  • Loading branch information
a2lin committed Nov 3, 2016
1 parent d77d4fa commit ca18747
Show file tree
Hide file tree
Showing 29 changed files with 495 additions and 174 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 @@ -41,10 +42,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 +60,13 @@
*/
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest {

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 +98,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 +119,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 +292,19 @@ 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) {
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 +313,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 +454,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 +467,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 +498,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 @@ -38,11 +40,16 @@ public PutIndexTemplateRequestBuilder(ElasticsearchClient client, PutIndexTempla
super(client, action, new PutIndexTemplateRequest(name));
}

@Deprecated
public PutIndexTemplateRequestBuilder setTemplate(String indexPattern) {
return setPatterns(Collections.singletonList(indexPattern));
}

/**
* 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.
*/
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 @@ -425,7 +425,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
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaData> {

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

public static final IndexTemplateMetaData PROTO = IndexTemplateMetaData.builder("").build();

private final String name;
Expand All @@ -56,7 +61,7 @@ public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaDat
* <pre><code>
* PUT /_template/my_template
* {
* "template": "my_index-*",
* "index_patterns": ["my_index-*"],
* "mappings": { ... },
* "version": 1
* }
Expand All @@ -70,7 +75,7 @@ public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaDat
@Nullable
private final Integer version;

private final String template;
private final List<String> patterns;

private final Settings settings;

Expand All @@ -82,14 +87,14 @@ public class IndexTemplateMetaData extends AbstractDiffable<IndexTemplateMetaDat
private final ImmutableOpenMap<String, IndexMetaData.Custom> customs;

public IndexTemplateMetaData(String name, int order, Integer version,
String template, Settings settings,
List<String> patterns, Settings settings,
ImmutableOpenMap<String, CompressedXContent> mappings,
ImmutableOpenMap<String, AliasMetaData> aliases,
ImmutableOpenMap<String, IndexMetaData.Custom> customs) {
this.name = name;
this.order = order;
this.version = version;
this.template = template;
this.patterns= patterns;
this.settings = settings;
this.mappings = mappings;
this.aliases = aliases;
Expand Down Expand Up @@ -122,12 +127,12 @@ public String getName() {
return this.name;
}

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

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

public Settings settings() {
Expand Down Expand Up @@ -182,7 +187,7 @@ public boolean equals(Object o) {
if (!mappings.equals(that.mappings)) return false;
if (!name.equals(that.name)) return false;
if (!settings.equals(that.settings)) return false;
if (!template.equals(that.template)) return false;
if (!patterns.equals(that.patterns)) return false;

return Objects.equals(version, that.version);
}
Expand All @@ -192,7 +197,7 @@ public int hashCode() {
int result = name.hashCode();
result = 31 * result + order;
result = 31 * result + Objects.hashCode(version);
result = 31 * result + template.hashCode();
result = 31 * result + patterns.hashCode();
result = 31 * result + settings.hashCode();
result = 31 * result + mappings.hashCode();
return result;
Expand All @@ -202,7 +207,11 @@ public int hashCode() {
public IndexTemplateMetaData readFrom(StreamInput in) throws IOException {
Builder builder = new Builder(in.readString());
builder.order(in.readInt());
builder.template(in.readString());
if (in.getVersion().onOrAfter(V_5_1_0)) {
builder.patterns(in.readList(StreamInput::readString));
} else {
builder.patterns(Collections.singletonList(in.readString()));
}
builder.settings(Settings.readSettingsFromStream(in));
int mappingsSize = in.readVInt();
for (int i = 0; i < mappingsSize; i++) {
Expand All @@ -229,7 +238,11 @@ public IndexTemplateMetaData readFrom(StreamInput in) throws IOException {
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeInt(order);
out.writeString(template);
if (out.getVersion().onOrAfter(V_5_1_0)) {
out.writeStringList(patterns);
} else {
out.writeString(patterns.size() > 0 ? patterns.get(0) : "");
}
Settings.writeSettingsToStream(settings, out);
out.writeVInt(mappings.size());
for (ObjectObjectCursor<String, CompressedXContent> cursor : mappings) {
Expand All @@ -252,7 +265,7 @@ public void writeTo(StreamOutput out) throws IOException {

public static class Builder {

private static final Set<String> VALID_FIELDS = Sets.newHashSet("template", "order", "mappings", "settings");
private static final Set<String> VALID_FIELDS = Sets.newHashSet("template", "order", "mappings", "settings", "index_patterns");
static {
VALID_FIELDS.addAll(IndexMetaData.customPrototypes.keySet());
}
Expand All @@ -263,7 +276,7 @@ public static class Builder {

private Integer version;

private String template;
private List<String> indexPatterns;

private Settings settings = Settings.Builder.EMPTY_SETTINGS;

Expand All @@ -284,7 +297,7 @@ public Builder(IndexTemplateMetaData indexTemplateMetaData) {
this.name = indexTemplateMetaData.name();
order(indexTemplateMetaData.order());
version(indexTemplateMetaData.version());
template(indexTemplateMetaData.template());
patterns(indexTemplateMetaData.patterns());
settings(indexTemplateMetaData.settings());

mappings = ImmutableOpenMap.builder(indexTemplateMetaData.mappings());
Expand All @@ -302,14 +315,11 @@ public Builder version(Integer version) {
return this;
}

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

public String template() {
return template;
}

public Builder settings(Settings.Builder settings) {
this.settings = settings.build();
Expand Down Expand Up @@ -361,7 +371,8 @@ public IndexMetaData.Custom getCustom(String type) {
}

public IndexTemplateMetaData build() {
return new IndexTemplateMetaData(name, order, version, template, settings, mappings.build(), aliases.build(), customs.build());
return new IndexTemplateMetaData(name, order, version, indexPatterns, settings, mappings.build(),
aliases.build(), customs.build());
}

@SuppressWarnings("unchecked")
Expand All @@ -373,7 +384,7 @@ public static void toXContent(IndexTemplateMetaData indexTemplateMetaData, XCont
if (indexTemplateMetaData.version() != null) {
builder.field("version", indexTemplateMetaData.version());
}
builder.field("template", indexTemplateMetaData.template());
builder.field("index_patterns", indexTemplateMetaData.patterns());

builder.startObject("settings");
indexTemplateMetaData.settings().toXContent(builder, params);
Expand Down Expand Up @@ -478,10 +489,17 @@ public static IndexTemplateMetaData fromXContent(XContentParser parser, String t
}
}
}
} else if ("index_patterns".equals(currentFieldName)) {
List<String> index_patterns = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
index_patterns.add(parser.text());
}
builder.patterns(index_patterns);
}
} else if (token.isValue()) {
// This is for roll-forward bwc with (#21009)
if ("template".equals(currentFieldName)) {
builder.template(parser.text());
builder.patterns(Collections.singletonList(parser.text()));
} else if ("order".equals(currentFieldName)) {
builder.order(parser.intValue());
} else if ("version".equals(currentFieldName)) {
Expand Down
Loading

0 comments on commit ca18747

Please sign in to comment.