Skip to content

Commit

Permalink
Added support for aliases to index templates
Browse files Browse the repository at this point in the history
Adapted existing PR (elastic#2739) to updated code (post elastic#4920), added tests and docs (@javanna)

Closes elastic#1825
  • Loading branch information
jbrook authored and javanna committed Mar 6, 2014
1 parent 9d0fc76 commit d302983
Show file tree
Hide file tree
Showing 19 changed files with 684 additions and 21 deletions.
29 changes: 29 additions & 0 deletions docs/reference/indices/templates.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,35 @@ Defines a template named template_1, with a template pattern of `te*`.
The settings and mappings will be applied to any index name that matches
the `te*` template.

coming[1.1.0]

It is also possible to include aliases in an index template as follows:

[source,js]
--------------------------------------------------
curl -XPUT localhost:9200/_template/template_1 -d '
{
"template" : "te*",
"settings" : {
"number_of_shards" : 1
},
"aliases" : {
"alias1" : {},
"alias2" : {
"filter" : {
"term" : {"user" : "kimchy" }
},
"routing" : "kimchy"
},
"{index}-alias" : {} <1>
}
}
'
--------------------------------------------------

<1> the `{index}` placeholder within the alias name will be replaced with the
actual index name that the template gets applied to during index creation.

[float]
[[delete]]
=== Deleting a Template
Expand Down
24 changes: 24 additions & 0 deletions rest-api-spec/test/indices.put_template/10_basic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,27 @@

- match: {test.template: "test-*"}
- match: {test.settings: {index.number_of_shards: '1', index.number_of_replicas: '0'}}

---
"Put template with aliases":
- do:
indices.put_template:
name: test
body:
template: test-*
aliases:
test_alias: {}
test_blias: {routing: b}
test_clias: {filter: {term :{user : kimchy}}}

- do:
indices.get_template:
name: test

- match: {test.template: "test-*"}
- length: {test.aliases: 3}
- is_true: test.aliases.test_alias
- match: {test.aliases.test_blias.index_routing: "b"}
- match: {test.aliases.test_blias.search_routing: "b"}
- match: {test.aliases.test_clias.filter.term.user: "kimchy"}

Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,27 @@
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.*;
import org.elasticsearch.common.xcontent.support.XContentMapValues;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import static com.google.common.collect.Maps.newHashMap;
import static com.google.common.collect.Sets.newHashSet;
import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.settings.ImmutableSettings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.settings.ImmutableSettings.readSettingsFromStream;
Expand All @@ -63,6 +66,8 @@ public class PutIndexTemplateRequest extends MasterNodeOperationRequest<PutIndex

private Map<String, String> mappings = newHashMap();

private final Set<Alias> aliases = newHashSet();

private Map<String, IndexMetaData.Custom> customs = newHashMap();

PutIndexTemplateRequest() {
Expand Down Expand Up @@ -251,6 +256,7 @@ public PutIndexTemplateRequest source(XContentBuilder templateBuilder) {
/**
* The template source definition.
*/
@SuppressWarnings("unchecked")
public PutIndexTemplateRequest source(Map templateSource) {
Map<String, Object> source = templateSource;
for (Map.Entry<String, Object> entry : source.entrySet()) {
Expand All @@ -272,6 +278,8 @@ public PutIndexTemplateRequest source(Map templateSource) {
}
mapping(entry1.getKey(), (Map<String, Object>) entry1.getValue());
}
} else if (name.equals("aliases")) {
aliases((Map<String, Object>) entry.getValue());
} else {
// maybe custom?
IndexMetaData.Custom.Factory factory = IndexMetaData.lookupFactory(name);
Expand Down Expand Up @@ -335,6 +343,66 @@ public PutIndexTemplateRequest custom(IndexMetaData.Custom custom) {
Map<String, IndexMetaData.Custom> customs() {
return this.customs;
}

Set<Alias> aliases() {
return this.aliases;
}

/**
* Sets the aliases that will be associated with the index when it gets created
*/
@SuppressWarnings("unchecked")
public PutIndexTemplateRequest aliases(Map source) {
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.map(source);
return aliases(builder.bytes());
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
}
}

/**
* Sets the aliases that will be associated with the index when it gets created
*/
public PutIndexTemplateRequest aliases(XContentBuilder source) {
return aliases(source.bytes());
}

/**
* Sets the aliases that will be associated with the index when it gets created
*/
public PutIndexTemplateRequest aliases(String source) {
return aliases(new BytesArray(source));
}

/**
* Sets the aliases that will be associated with the index when it gets created
*/
public PutIndexTemplateRequest aliases(BytesReference source) {
try {
XContentParser parser = XContentHelper.createParser(source);
//move to the first alias
parser.nextToken();
while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) {
alias(Alias.fromXContent(parser));
}
return this;
} catch(IOException e) {
throw new ElasticsearchParseException("Failed to parse aliases", e);
}
}

/**
* Adds an alias that will be added when the index gets created.
*
* @param alias The metadata for the new alias
* @return the index template creation request
*/
public PutIndexTemplateRequest alias(Alias alias) {
aliases.add(alias);
return this;
}

@Override
public void readFrom(StreamInput in) throws IOException {
Expand All @@ -355,6 +423,12 @@ public void readFrom(StreamInput in) throws IOException {
IndexMetaData.Custom customIndexMetaData = IndexMetaData.lookupFactorySafe(type).readFrom(in);
customs.put(type, customIndexMetaData);
}
if (in.getVersion().onOrAfter(Version.V_1_1_0)) {
int aliasesSize = in.readVInt();
for (int i = 0; i < aliasesSize; i++) {
aliases.add(Alias.read(in));
}
}
}

@Override
Expand All @@ -376,5 +450,11 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(entry.getKey());
IndexMetaData.lookupFactorySafe(entry.getKey()).writeTo(entry.getValue(), out);
}
if (out.getVersion().onOrAfter(Version.V_1_1_0)) {
out.writeVInt(aliases.size());
for (Alias alias : aliases) {
alias.writeTo(out);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.elasticsearch.action.admin.indices.template.put;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.internal.InternalIndicesAdminClient;
Expand Down Expand Up @@ -109,6 +110,49 @@ public PutIndexTemplateRequestBuilder addMapping(String type, String source) {
return this;
}

/**
* Sets the aliases that will be associated with the index when it gets created
*/
public PutIndexTemplateRequestBuilder setAliases(Map source) {
request.aliases(source);
return this;
}

/**
* Sets the aliases that will be associated with the index when it gets created
*/
public PutIndexTemplateRequestBuilder setAliases(String source) {
request.aliases(source);
return this;
}

/**
* Sets the aliases that will be associated with the index when it gets created
*/
public PutIndexTemplateRequestBuilder setAliases(XContentBuilder source) {
request.aliases(source);
return this;
}

/**
* Sets the aliases that will be associated with the index when it gets created
*/
public PutIndexTemplateRequestBuilder setAliases(BytesReference source) {
request.aliases(source);
return this;
}

/**
* Adds an alias that will be added when the index template gets created.
*
* @param alias The alias
* @return the request builder
*/
public PutIndexTemplateRequestBuilder addAlias(Alias alias) {
request.alias(alias);
return this;
}

/**
* The cause for this index template creation.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ protected void masterOperation(final PutIndexTemplateRequest request, final Clus
.order(request.order())
.settings(request.settings())
.mappings(request.mappings())
.aliases(request.aliases())
.customs(request.customs())
.create(request.create())
.masterTimeout(request.masterNodeTimeout()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ private AliasMetaData(String alias, CompressedString filter, String indexRouting
}
}

private AliasMetaData(AliasMetaData aliasMetaData, String alias) {
this(alias, aliasMetaData.filter(), aliasMetaData.indexRouting(), aliasMetaData.searchRouting());
}

public String alias() {
return alias;
}
Expand Down Expand Up @@ -110,6 +114,13 @@ public static Builder newAliasMetaDataBuilder(String alias) {
return new Builder(alias);
}

/**
* Creates a new AliasMetaData instance with same content as the given one, but with a different alias name
*/
public static AliasMetaData newAliasMetaData(AliasMetaData aliasMetaData, String newAlias) {
return new AliasMetaData(aliasMetaData, newAlias);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -137,7 +148,7 @@ public int hashCode() {

public static class Builder {

private String alias;
private final String alias;

private CompressedString filter;

Expand Down
Loading

0 comments on commit d302983

Please sign in to comment.