Skip to content

Commit

Permalink
Persist all template alias settings on PUT template (#63162)
Browse files Browse the repository at this point in the history
If the templates contains an alias the fields is_write_index and
is_hidden are now copied to the metadata object and persisted
  • Loading branch information
davidkyle authored Oct 13, 2020
1 parent 1a1d26e commit d5f60b5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,13 @@ static ClusterState innerPutTemplate(final ClusterState currentState, PutRequest
}

for (Alias alias : request.aliases) {
AliasMetadata aliasMetadata = AliasMetadata.builder(alias.name()).filter(alias.filter())
.indexRouting(alias.indexRouting()).searchRouting(alias.searchRouting()).build();
AliasMetadata aliasMetadata = AliasMetadata.builder(alias.name())
.filter(alias.filter())
.indexRouting(alias.indexRouting())
.searchRouting(alias.searchRouting())
.writeIndex(alias.writeIndex())
.isHidden(alias.isHidden())
.build();
templateBuilder.putAlias(aliasMetadata);
}
IndexTemplateMetadata template = templateBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.cluster.metadata.MetadataIndexTemplateService.PutRequest;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -228,6 +229,38 @@ public void testAliasInvalidFilterInvalidJson() throws Exception {
assertThat(errors.get(0).getMessage(), equalTo("failed to parse filter for alias [invalid_alias]"));
}

public void testIndexTemplateWithAlias() throws Exception {
final String templateName = "template_with_alias";
final String aliasName = "alias_with_settings";
PutRequest request = new PutRequest("api", templateName);
request.patterns(singletonList("te*"));
request.mappings("{}");
Alias alias = new Alias(aliasName)
.filter(randomBoolean() ? null : "{\"term\":{\"user_id\":12}}")
.indexRouting(randomBoolean() ? null : "route1")
.searchRouting(randomBoolean() ? null :"route2")
.isHidden(randomBoolean() ? null : randomBoolean())
.writeIndex(randomBoolean() ? null : randomBoolean());
Set<Alias> aliases = new HashSet<>();
aliases.add(alias);
request.aliases(aliases);

List<Throwable> errors = putTemplateDetail(request);
assertThat(errors, is(empty()));

final Metadata metadata = client().admin().cluster().prepareState().get().getState().metadata();
IndexTemplateMetadata template = metadata.templates().get(templateName);
ImmutableOpenMap<String, AliasMetadata> aliasMap = template.getAliases();
assertThat(aliasMap.size(), equalTo(1));
AliasMetadata metaAlias = aliasMap.get(aliasName);
String filterString = metaAlias.filter() == null ? null : metaAlias.filter().string();
assertThat(filterString, equalTo(alias.filter()));
assertThat(metaAlias.indexRouting(), equalTo(alias.indexRouting()));
assertThat(metaAlias.searchRouting(), equalTo(alias.searchRouting()));
assertThat(metaAlias.isHidden(), equalTo(alias.isHidden()));
assertThat(metaAlias.writeIndex(), equalTo(alias.writeIndex()));
}

public void testFindTemplates() throws Exception {
client().admin().indices().prepareDeleteTemplate("*").get(); // Delete all existing templates
putTemplateDetail(new PutRequest("test", "foo-1").patterns(singletonList("foo-*")).order(1));
Expand Down

0 comments on commit d5f60b5

Please sign in to comment.