diff --git a/docs/changelog/120453.yaml b/docs/changelog/120453.yaml new file mode 100644 index 0000000000000..48b34019f855d --- /dev/null +++ b/docs/changelog/120453.yaml @@ -0,0 +1,5 @@ +pr: 120453 +summary: Support boolean strings for alias defination in CreateIndex API +area: Indices APIs +type: bug +issues: [] diff --git a/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.create/10_basic.yml b/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.create/10_basic.yml index 8645c91a51ad3..acf5641b4eecc 100644 --- a/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.create/10_basic.yml +++ b/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.create/10_basic.yml @@ -99,6 +99,8 @@ is_write_index: false test_clias: is_write_index: true + test_dlias: + is_write_index: "true" - do: indices.get_alias: @@ -107,6 +109,7 @@ - is_false: test_index.aliases.test_alias.is_write_index - is_false: test_index.aliases.test_blias.is_write_index - is_true: test_index.aliases.test_clias.is_write_index + - is_true: test_index.aliases.test_dlias.is_write_index --- "Create index with invalid mappings": diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/alias/Alias.java b/server/src/main/java/org/elasticsearch/action/admin/indices/alias/Alias.java index a6959e5b59e06..b75aed3b08d7d 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/alias/Alias.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/alias/Alias.java @@ -42,6 +42,14 @@ public class Alias implements Writeable, ToXContentFragment { private static final ParseField SEARCH_ROUTING = new ParseField("search_routing", "searchRouting", "search-routing"); private static final ParseField IS_WRITE_INDEX = new ParseField("is_write_index"); private static final ParseField IS_HIDDEN = new ParseField("is_hidden"); + private static final Set KNOWN_FIELDS = Set.of( + FILTER.getPreferredName(), + ROUTING.getPreferredName(), + INDEX_ROUTING.getPreferredName(), + SEARCH_ROUTING.getPreferredName(), + IS_WRITE_INDEX.getPreferredName(), + IS_HIDDEN.getPreferredName() + ); private String name; @@ -144,8 +152,12 @@ public Alias filter(QueryBuilder filterBuilder) { * Associates a routing value to the alias */ public Alias routing(String routing) { - this.indexRouting = routing; - this.searchRouting = routing; + if (this.indexRouting == null) { + this.indexRouting = routing; + } + if (this.searchRouting == null) { + this.searchRouting = routing; + } return this; } @@ -234,23 +246,14 @@ public static Alias fromXContent(XContentParser parser) throws IOException { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); // check if there are any unknown fields - Set knownFieldNames = Set.of( - FILTER.getPreferredName(), - ROUTING.getPreferredName(), - INDEX_ROUTING.getPreferredName(), - SEARCH_ROUTING.getPreferredName(), - IS_WRITE_INDEX.getPreferredName(), - IS_HIDDEN.getPreferredName() - ); - if (knownFieldNames.contains(currentFieldName) == false) { + if (KNOWN_FIELDS.contains(currentFieldName) == false) { throw new IllegalArgumentException("Unknown field [" + currentFieldName + "] in alias [" + alias.name + "]"); } } else if (token == XContentParser.Token.START_OBJECT) { if (FILTER.match(currentFieldName, parser.getDeprecationHandler())) { - Map filter = parser.mapOrdered(); - alias.filter(filter); + alias.filter(parser.mapOrdered()); } - } else if (token == XContentParser.Token.VALUE_STRING) { + } else if (token.isValue()) { if (ROUTING.match(currentFieldName, parser.getDeprecationHandler())) { alias.routing(parser.text()); } else if (INDEX_ROUTING.match(currentFieldName, parser.getDeprecationHandler())) { @@ -258,7 +261,6 @@ public static Alias fromXContent(XContentParser parser) throws IOException { } else if (SEARCH_ROUTING.match(currentFieldName, parser.getDeprecationHandler())) { alias.searchRouting(parser.text()); } - } else if (token == XContentParser.Token.VALUE_BOOLEAN) { if (IS_WRITE_INDEX.match(currentFieldName, parser.getDeprecationHandler())) { alias.writeIndex(parser.booleanValue()); } else if (IS_HIDDEN.match(currentFieldName, parser.getDeprecationHandler())) { diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndexPutAliasAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndexPutAliasAction.java index f68fbaee3e907..045a51965d350 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndexPutAliasAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndexPutAliasAction.java @@ -95,6 +95,8 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC if ("filter".equals(currentFieldName)) { filter = parser.mapOrdered(); } + } else { + throw new IllegalArgumentException("Unknown token [" + token + "]"); } } }