Skip to content

Commit 5b2ab96

Browse files
committed
Return index name and empty map for /{index}/_alias with no aliases
Previously in #24723 we changed the `_alias` API to not go through the `RestGetIndicesAction` endpoint, instead creating a `RestGetAliasesAction` that did the same thing. This changes the formatting so that it matches the old formatting of the endpoint, before: ``` GET /test-1/_alias { } ``` And after this change: ``` GET /test-1/_alias { "test-1": { "aliases": {} } } ``` This is related to #25090
1 parent ee0e921 commit 5b2ab96

File tree

7 files changed

+126
-17
lines changed

7 files changed

+126
-17
lines changed

core/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ public SortedMap<String, AliasOrIndex> getAliasAndIndexLookup() {
243243
*
244244
* @param aliases The names of the index aliases to find
245245
* @param concreteIndices The concrete indexes the index aliases must point to order to be returned.
246-
* @return the found index aliases grouped by index
246+
* @return a map of index to a list of alias metadata, the list corresponding to a concrete index will be empty if no aliases are
247+
* present for that index
247248
*/
248249
public ImmutableOpenMap<String, List<AliasMetaData>> findAliases(final String[] aliases, String[] concreteIndices) {
249250
assert aliases != null;
@@ -273,8 +274,8 @@ public int compare(AliasMetaData o1, AliasMetaData o2) {
273274
return o1.alias().compareTo(o2.alias());
274275
}
275276
});
276-
mapBuilder.put(index, Collections.unmodifiableList(filteredValues));
277277
}
278+
mapBuilder.put(index, Collections.unmodifiableList(filteredValues));
278279
}
279280
return mapBuilder.build();
280281
}

core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public String getName() {
7676

7777
@Override
7878
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
79+
final boolean namesProvided = request.hasParam("name");
7980
final String[] aliases = request.paramAsStringArrayOrEmptyIfAll("name");
8081
final GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases);
8182
final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
@@ -89,9 +90,13 @@ public RestResponse buildResponse(GetAliasesResponse response, XContentBuilder b
8990
final ImmutableOpenMap<String, List<AliasMetaData>> aliasMap = response.getAliases();
9091

9192
final Set<String> aliasNames = new HashSet<>();
92-
for (final ObjectCursor<List<AliasMetaData>> cursor : aliasMap.values()) {
93+
final Set<String> indicesToDisplay = new HashSet<>();
94+
for (final ObjectObjectCursor<String, List<AliasMetaData>> cursor : aliasMap) {
9395
for (final AliasMetaData aliasMetaData : cursor.value) {
9496
aliasNames.add(aliasMetaData.alias());
97+
if (namesProvided) {
98+
indicesToDisplay.add(cursor.key);
99+
}
95100
}
96101
}
97102

@@ -131,17 +136,19 @@ public RestResponse buildResponse(GetAliasesResponse response, XContentBuilder b
131136
}
132137

133138
for (final ObjectObjectCursor<String, List<AliasMetaData>> entry : response.getAliases()) {
134-
builder.startObject(entry.key);
135-
{
136-
builder.startObject("aliases");
139+
if (namesProvided == false || (namesProvided && indicesToDisplay.contains(entry.key))) {
140+
builder.startObject(entry.key);
137141
{
138-
for (final AliasMetaData alias : entry.value) {
139-
AliasMetaData.Builder.toXContent(alias, builder, ToXContent.EMPTY_PARAMS);
142+
builder.startObject("aliases");
143+
{
144+
for (final AliasMetaData alias : entry.value) {
145+
AliasMetaData.Builder.toXContent(alias, builder, ToXContent.EMPTY_PARAMS);
146+
}
140147
}
148+
builder.endObject();
141149
}
142150
builder.endObject();
143151
}
144-
builder.endObject();
145152
}
146153
}
147154
builder.endObject();

core/src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexIT.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.action.admin.indices.get;
2121

22+
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
2223
import org.elasticsearch.action.admin.indices.alias.Alias;
2324
import org.elasticsearch.action.admin.indices.get.GetIndexRequest.Feature;
2425
import org.elasticsearch.cluster.metadata.AliasMetaData;
@@ -281,6 +282,8 @@ private void assertEmptyMappings(GetIndexResponse response) {
281282

282283
private void assertEmptyAliases(GetIndexResponse response) {
283284
assertThat(response.aliases(), notNullValue());
284-
assertThat(response.aliases().isEmpty(), equalTo(true));
285+
for (final ObjectObjectCursor<String, List<AliasMetaData>> entry : response.getAliases()) {
286+
assertTrue(entry.value.isEmpty());
287+
}
285288
}
286289
}

core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.aliases;
2121

22+
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
2223
import org.elasticsearch.action.admin.indices.alias.Alias;
2324
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions;
2425
import org.elasticsearch.action.admin.indices.alias.exists.AliasesExistResponse;
@@ -32,6 +33,7 @@
3233
import org.elasticsearch.cluster.metadata.AliasOrIndex;
3334
import org.elasticsearch.cluster.metadata.IndexMetaData;
3435
import org.elasticsearch.common.StopWatch;
36+
import org.elasticsearch.common.Strings;
3537
import org.elasticsearch.common.settings.Settings;
3638
import org.elasticsearch.common.unit.TimeValue;
3739
import org.elasticsearch.common.xcontent.XContentType;
@@ -49,6 +51,7 @@
4951

5052
import java.util.Arrays;
5153
import java.util.HashSet;
54+
import java.util.List;
5255
import java.util.Set;
5356
import java.util.concurrent.ExecutionException;
5457
import java.util.concurrent.ExecutorService;
@@ -567,20 +570,24 @@ public void testIndicesGetAliases() throws Exception {
567570
logger.info("--> getting alias1");
568571
GetAliasesResponse getResponse = admin().indices().prepareGetAliases("alias1").get();
569572
assertThat(getResponse, notNullValue());
570-
assertThat(getResponse.getAliases().size(), equalTo(1));
573+
assertThat(getResponse.getAliases().size(), equalTo(5));
571574
assertThat(getResponse.getAliases().get("foobar").size(), equalTo(1));
572575
assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
573576
assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("alias1"));
574577
assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue());
575578
assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue());
576579
assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
580+
assertTrue(getResponse.getAliases().get("test").isEmpty());
581+
assertTrue(getResponse.getAliases().get("test123").isEmpty());
582+
assertTrue(getResponse.getAliases().get("foobarbaz").isEmpty());
583+
assertTrue(getResponse.getAliases().get("bazbar").isEmpty());
577584
AliasesExistResponse existsResponse = admin().indices().prepareAliasesExist("alias1").get();
578585
assertThat(existsResponse.exists(), equalTo(true));
579586

580587
logger.info("--> getting all aliases that start with alias*");
581588
getResponse = admin().indices().prepareGetAliases("alias*").get();
582589
assertThat(getResponse, notNullValue());
583-
assertThat(getResponse.getAliases().size(), equalTo(1));
590+
assertThat(getResponse.getAliases().size(), equalTo(5));
584591
assertThat(getResponse.getAliases().get("foobar").size(), equalTo(2));
585592
assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
586593
assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("alias1"));
@@ -592,6 +599,10 @@ public void testIndicesGetAliases() throws Exception {
592599
assertThat(getResponse.getAliases().get("foobar").get(1).getFilter(), nullValue());
593600
assertThat(getResponse.getAliases().get("foobar").get(1).getIndexRouting(), nullValue());
594601
assertThat(getResponse.getAliases().get("foobar").get(1).getSearchRouting(), nullValue());
602+
assertTrue(getResponse.getAliases().get("test").isEmpty());
603+
assertTrue(getResponse.getAliases().get("test123").isEmpty());
604+
assertTrue(getResponse.getAliases().get("foobarbaz").isEmpty());
605+
assertTrue(getResponse.getAliases().get("bazbar").isEmpty());
595606
existsResponse = admin().indices().prepareAliasesExist("alias*").get();
596607
assertThat(existsResponse.exists(), equalTo(true));
597608

@@ -676,12 +687,13 @@ public void testIndicesGetAliases() throws Exception {
676687
logger.info("--> getting f* for index *bar");
677688
getResponse = admin().indices().prepareGetAliases("f*").addIndices("*bar").get();
678689
assertThat(getResponse, notNullValue());
679-
assertThat(getResponse.getAliases().size(), equalTo(1));
690+
assertThat(getResponse.getAliases().size(), equalTo(2));
680691
assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
681692
assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo"));
682693
assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue());
683694
assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue());
684695
assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
696+
assertTrue(getResponse.getAliases().get("bazbar").isEmpty());
685697
existsResponse = admin().indices().prepareAliasesExist("f*")
686698
.addIndices("*bar").get();
687699
assertThat(existsResponse.exists(), equalTo(true));
@@ -690,13 +702,14 @@ public void testIndicesGetAliases() throws Exception {
690702
logger.info("--> getting f* for index *bac");
691703
getResponse = admin().indices().prepareGetAliases("foo").addIndices("*bac").get();
692704
assertThat(getResponse, notNullValue());
693-
assertThat(getResponse.getAliases().size(), equalTo(1));
705+
assertThat(getResponse.getAliases().size(), equalTo(2));
694706
assertThat(getResponse.getAliases().get("foobar").size(), equalTo(1));
695707
assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
696708
assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo"));
697709
assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue());
698710
assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue());
699711
assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
712+
assertTrue(getResponse.getAliases().get("bazbar").isEmpty());
700713
existsResponse = admin().indices().prepareAliasesExist("foo")
701714
.addIndices("*bac").get();
702715
assertThat(existsResponse.exists(), equalTo(true));
@@ -729,7 +742,9 @@ public void testIndicesGetAliases() throws Exception {
729742
.removeAlias("foobar", "foo"));
730743

731744
getResponse = admin().indices().prepareGetAliases("foo").addIndices("foobar").get();
732-
assertThat(getResponse.getAliases().isEmpty(), equalTo(true));
745+
for (final ObjectObjectCursor<String, List<AliasMetaData>> entry : getResponse.getAliases()) {
746+
assertTrue(entry.value.isEmpty());
747+
}
733748
existsResponse = admin().indices().prepareAliasesExist("foo").addIndices("foobar").get();
734749
assertThat(existsResponse.exists(), equalTo(false));
735750
}

rest-api-spec/src/main/resources/rest-api-spec/test/indices.delete_alias/all_path_options.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ setup:
8484

8585
---
8686
"check delete with index list":
87+
- skip:
88+
version: " - 5.99.99"
89+
reason: only requested indices are included in 6.x
90+
8791
- do:
8892
indices.delete_alias:
8993
index: "test_index1,test_index2"
@@ -106,6 +110,10 @@ setup:
106110

107111
---
108112
"check delete with prefix* index":
113+
- skip:
114+
version: " - 5.99.99"
115+
reason: only requested indices are included in 6.x
116+
109117
- do:
110118
indices.delete_alias:
111119
index: "test_*"
@@ -129,6 +137,10 @@ setup:
129137

130138
---
131139
"check delete with index list and * aliases":
140+
- skip:
141+
version: " - 5.99.99"
142+
reason: only requested indices are included in 6.x
143+
132144
- do:
133145
indices.delete_alias:
134146
index: "test_index1,test_index2"
@@ -152,6 +164,10 @@ setup:
152164

153165
---
154166
"check delete with index list and _all aliases":
167+
- skip:
168+
version: " - 5.99.99"
169+
reason: only requested indices are included in 6.x
170+
155171
- do:
156172
indices.delete_alias:
157173
index: "test_index1,test_index2"
@@ -175,6 +191,10 @@ setup:
175191

176192
---
177193
"check delete with index list and wildcard aliases":
194+
- skip:
195+
version: " - 5.99.99"
196+
reason: only requested indices are included in 6.x
197+
178198
- do:
179199
indices.delete_alias:
180200
index: "test_index1,test_index2"

rest-api-spec/src/main/resources/rest-api-spec/test/indices.get_alias/10_basic.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,62 @@ setup:
4040
- match: {test_index.aliases.test_blias: {}}
4141
- is_false: test_index_2
4242

43+
---
44+
"Get aliases via /_all/_alias/":
45+
- skip:
46+
version: " - 5.99.99"
47+
reason: only requested indices are included in 6.x
48+
49+
- do:
50+
indices.create:
51+
index: myindex
52+
53+
- do:
54+
indices.get_alias:
55+
index: _all
56+
57+
- match: {test_index.aliases.test_alias: {}}
58+
- match: {test_index.aliases.test_blias: {}}
59+
- match: {test_index_2.aliases.test_alias: {}}
60+
- match: {test_index_2.aliases.test_blias: {}}
61+
- match: {myindex.aliases: {}}
62+
63+
---
64+
"Get aliases via /*/_alias/":
65+
- skip:
66+
version: " - 5.99.99"
67+
reason: only requested indices are included in 6.x
68+
69+
- do:
70+
indices.create:
71+
index: myindex
72+
73+
- do:
74+
indices.get_alias:
75+
index: "*"
76+
77+
- match: {test_index.aliases.test_alias: {}}
78+
- match: {test_index.aliases.test_blias: {}}
79+
- match: {test_index_2.aliases.test_alias: {}}
80+
- match: {test_index_2.aliases.test_blias: {}}
81+
- match: {myindex.aliases: {}}
82+
83+
---
84+
"Get and index with no aliases via /{index}/_alias/":
85+
- skip:
86+
version: " - 5.99.99"
87+
reason: only requested indices are included in 6.x
88+
89+
- do:
90+
indices.create:
91+
index: myindex
92+
93+
- do:
94+
indices.get_alias:
95+
index: myindex
96+
97+
- match: {myindex.aliases: {}}
98+
4399
---
44100
"Get specific alias via /{index}/_alias/{name}":
45101

rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_alias/all_path_options.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ setup:
1414

1515
---
1616
"put alias per index":
17+
- skip:
18+
version: " - 5.99.99"
19+
reason: only requested indices are included in 6.x
1720

1821
- do:
1922
indices.put_alias:
@@ -69,7 +72,9 @@ setup:
6972

7073
---
7174
"put alias prefix* index":
72-
75+
- skip:
76+
version: " - 5.99.99"
77+
reason: only requested indices are included in 6.x
7378

7479
- do:
7580
indices.put_alias:
@@ -86,7 +91,9 @@ setup:
8691

8792
---
8893
"put alias in list of indices":
89-
94+
- skip:
95+
version: " - 5.99.99"
96+
reason: only requested indices are included in 6.x
9097

9198
- do:
9299
indices.put_alias:

0 commit comments

Comments
 (0)