Skip to content

Commit 175983d

Browse files
authored
Fix the use of wildcard expressions for data streams in update aliases api (#75562)
Backporting #75526 to 7.x branch. Prior to this change, supplying a wildcard expression in the `indices` field of an alias action would always result in a 404, despite data streams existing that could match with the provided wildcard expression. Closes #75456
1 parent 59ef3df commit 175983d

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.Version;
1313
import org.elasticsearch.action.ActionRequestValidationException;
1414
import org.elasticsearch.action.AliasesRequest;
15+
import org.elasticsearch.action.IndicesRequest;
1516
import org.elasticsearch.action.support.IndicesOptions;
1617
import org.elasticsearch.action.support.master.AcknowledgedRequest;
1718
import org.elasticsearch.cluster.metadata.AliasAction;
@@ -49,7 +50,7 @@
4950
/**
5051
* A request to add/remove aliases for one or more indices.
5152
*/
52-
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> implements ToXContentObject {
53+
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> implements IndicesRequest, ToXContentObject {
5354

5455
private List<AliasActions> allAliasActions = new ArrayList<>();
5556
private String origin = "";
@@ -505,6 +506,11 @@ public String[] indices() {
505506
return indices;
506507
}
507508

509+
@Override
510+
public boolean includeDataStreams() {
511+
return true;
512+
}
513+
508514
@Override
509515
public IndicesOptions indicesOptions() {
510516
return INDICES_OPTIONS;
@@ -643,6 +649,18 @@ public IndicesOptions indicesOptions() {
643649
return INDICES_OPTIONS;
644650
}
645651

652+
@Override
653+
public String[] indices() {
654+
return allAliasActions.stream()
655+
.flatMap(aliasActions -> Arrays.stream(aliasActions.indices()))
656+
.toArray(String[]::new);
657+
}
658+
659+
@Override
660+
public boolean includeDataStreams() {
661+
return true;
662+
}
663+
646664
@Override
647665
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
648666
builder.startObject();

server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,24 +1797,21 @@ public void testIndicesAliasesRequestTargetDataStreams() {
17971797

17981798
{
17991799
IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions.add().index(dataStreamName);
1800-
IndexNotFoundException iae = expectThrows(IndexNotFoundException.class,
1801-
() -> indexNameExpressionResolver.concreteIndexNames(state, aliasActions));
1802-
assertEquals("no such index [" + dataStreamName + "]", iae.getMessage());
1800+
assertThat(indexNameExpressionResolver.concreteIndexNames(state, aliasActions),
1801+
arrayContaining(backingIndexEqualTo(dataStreamName, 1)));
18031802
}
18041803

18051804
{
18061805
IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions.add().index("my-data-*").alias("my-data");
1807-
IndexNotFoundException iae = expectThrows(IndexNotFoundException.class,
1808-
() -> indexNameExpressionResolver.concreteIndexNames(state, aliasActions));
1809-
assertEquals("no such index [my-data-*]", iae.getMessage());
1806+
assertThat(indexNameExpressionResolver.concreteIndexNames(state, aliasActions),
1807+
arrayContaining(backingIndexEqualTo(dataStreamName, 1)));
18101808
}
18111809

18121810
{
18131811
IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions.add().index(dataStreamName)
18141812
.alias("my-data");
1815-
IndexNotFoundException iae = expectThrows(IndexNotFoundException.class,
1816-
() -> indexNameExpressionResolver.concreteIndexNames(state, aliasActions));
1817-
assertEquals("no such index [" + dataStreamName + "]", iae.getMessage());
1813+
assertThat(indexNameExpressionResolver.concreteIndexNames(state, aliasActions),
1814+
arrayContaining(backingIndexEqualTo(dataStreamName, 1)));
18181815
}
18191816
}
18201817

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/data_stream/140_data_stream_aliases.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,45 @@
152152
search:
153153
index: app1-zone-b
154154
- length: { hits.hits: 1 }
155+
156+
---
157+
"Create data stream aliases using wildcard expression":
158+
- skip:
159+
version: " - 7.99.99"
160+
reason: "bugfix has not yet backported to the 7.x branch"
161+
features: allowed_warnings
162+
163+
- do:
164+
allowed_warnings:
165+
- "index template [my-template] has index patterns [log-*] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template] will take precedence during new index creation"
166+
indices.put_index_template:
167+
name: my-template
168+
body:
169+
index_patterns: [ log-* ]
170+
template:
171+
settings:
172+
index.number_of_replicas: 0
173+
data_stream: { }
174+
175+
- do:
176+
indices.create_data_stream:
177+
name: log-foobar
178+
- is_true: acknowledged
179+
180+
- do:
181+
indices.update_aliases:
182+
body:
183+
actions:
184+
- add:
185+
index: log-*
186+
alias: my-alias
187+
- is_true: acknowledged
188+
189+
- do:
190+
indices.get_data_stream:
191+
name: "*"
192+
- match: { data_streams.0.name: log-foobar }
193+
194+
- do:
195+
indices.get_alias: {}
196+
- match: {log-foobar.aliases.my-alias: {}}

0 commit comments

Comments
 (0)