Skip to content

Return index name and empty map for /{index}/_alias with no aliases #25114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ public SortedMap<String, AliasOrIndex> getAliasAndIndexLookup() {
*
* @param aliases The names of the index aliases to find
* @param concreteIndices The concrete indexes the index aliases must point to order to be returned.
* @return the found index aliases grouped by index
* @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
* present for that index
*/
public ImmutableOpenMap<String, List<AliasMetaData>> findAliases(final String[] aliases, String[] concreteIndices) {
assert aliases != null;
Expand Down Expand Up @@ -273,8 +274,8 @@ public int compare(AliasMetaData o1, AliasMetaData o2) {
return o1.alias().compareTo(o2.alias());
}
});
mapBuilder.put(index, Collections.unmodifiableList(filteredValues));
}
mapBuilder.put(index, Collections.unmodifiableList(filteredValues));
}
return mapBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public String getName() {

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

final Set<String> aliasNames = new HashSet<>();
for (final ObjectCursor<List<AliasMetaData>> cursor : aliasMap.values()) {
final Set<String> indicesToDisplay = new HashSet<>();
for (final ObjectObjectCursor<String, List<AliasMetaData>> cursor : aliasMap) {
for (final AliasMetaData aliasMetaData : cursor.value) {
aliasNames.add(aliasMetaData.alias());
if (namesProvided) {
indicesToDisplay.add(cursor.key);
}
}
}

Expand Down Expand Up @@ -131,17 +136,19 @@ public RestResponse buildResponse(GetAliasesResponse response, XContentBuilder b
}

for (final ObjectObjectCursor<String, List<AliasMetaData>> entry : response.getAliases()) {
builder.startObject(entry.key);
{
builder.startObject("aliases");
if (namesProvided == false || (namesProvided && indicesToDisplay.contains(entry.key))) {
builder.startObject(entry.key);
{
for (final AliasMetaData alias : entry.value) {
AliasMetaData.Builder.toXContent(alias, builder, ToXContent.EMPTY_PARAMS);
builder.startObject("aliases");
{
for (final AliasMetaData alias : entry.value) {
AliasMetaData.Builder.toXContent(alias, builder, ToXContent.EMPTY_PARAMS);
}
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
}
}
builder.endObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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

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

private void assertEmptyAliases(GetIndexResponse response) {
assertThat(response.aliases(), notNullValue());
assertThat(response.aliases().isEmpty(), equalTo(true));
for (final ObjectObjectCursor<String, List<AliasMetaData>> entry : response.getAliases()) {
assertTrue(entry.value.isEmpty());
}
}
}
25 changes: 20 additions & 5 deletions core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.aliases;

import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions;
import org.elasticsearch.action.admin.indices.alias.exists.AliasesExistResponse;
Expand All @@ -32,6 +33,7 @@
import org.elasticsearch.cluster.metadata.AliasOrIndex;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.StopWatch;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
Expand All @@ -49,6 +51,7 @@

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -567,20 +570,24 @@ public void testIndicesGetAliases() throws Exception {
logger.info("--> getting alias1");
GetAliasesResponse getResponse = admin().indices().prepareGetAliases("alias1").get();
assertThat(getResponse, notNullValue());
assertThat(getResponse.getAliases().size(), equalTo(1));
assertThat(getResponse.getAliases().size(), equalTo(5));
assertThat(getResponse.getAliases().get("foobar").size(), equalTo(1));
assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("alias1"));
assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
assertTrue(getResponse.getAliases().get("test").isEmpty());
assertTrue(getResponse.getAliases().get("test123").isEmpty());
assertTrue(getResponse.getAliases().get("foobarbaz").isEmpty());
assertTrue(getResponse.getAliases().get("bazbar").isEmpty());
AliasesExistResponse existsResponse = admin().indices().prepareAliasesExist("alias1").get();
assertThat(existsResponse.exists(), equalTo(true));

logger.info("--> getting all aliases that start with alias*");
getResponse = admin().indices().prepareGetAliases("alias*").get();
assertThat(getResponse, notNullValue());
assertThat(getResponse.getAliases().size(), equalTo(1));
assertThat(getResponse.getAliases().size(), equalTo(5));
assertThat(getResponse.getAliases().get("foobar").size(), equalTo(2));
assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("alias1"));
Expand All @@ -592,6 +599,10 @@ public void testIndicesGetAliases() throws Exception {
assertThat(getResponse.getAliases().get("foobar").get(1).getFilter(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(1).getIndexRouting(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(1).getSearchRouting(), nullValue());
assertTrue(getResponse.getAliases().get("test").isEmpty());
assertTrue(getResponse.getAliases().get("test123").isEmpty());
assertTrue(getResponse.getAliases().get("foobarbaz").isEmpty());
assertTrue(getResponse.getAliases().get("bazbar").isEmpty());
existsResponse = admin().indices().prepareAliasesExist("alias*").get();
assertThat(existsResponse.exists(), equalTo(true));

Expand Down Expand Up @@ -676,12 +687,13 @@ public void testIndicesGetAliases() throws Exception {
logger.info("--> getting f* for index *bar");
getResponse = admin().indices().prepareGetAliases("f*").addIndices("*bar").get();
assertThat(getResponse, notNullValue());
assertThat(getResponse.getAliases().size(), equalTo(1));
assertThat(getResponse.getAliases().size(), equalTo(2));
assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo"));
assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
assertTrue(getResponse.getAliases().get("bazbar").isEmpty());
existsResponse = admin().indices().prepareAliasesExist("f*")
.addIndices("*bar").get();
assertThat(existsResponse.exists(), equalTo(true));
Expand All @@ -690,13 +702,14 @@ public void testIndicesGetAliases() throws Exception {
logger.info("--> getting f* for index *bac");
getResponse = admin().indices().prepareGetAliases("foo").addIndices("*bac").get();
assertThat(getResponse, notNullValue());
assertThat(getResponse.getAliases().size(), equalTo(1));
assertThat(getResponse.getAliases().size(), equalTo(2));
assertThat(getResponse.getAliases().get("foobar").size(), equalTo(1));
assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo"));
assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
assertTrue(getResponse.getAliases().get("bazbar").isEmpty());
existsResponse = admin().indices().prepareAliasesExist("foo")
.addIndices("*bac").get();
assertThat(existsResponse.exists(), equalTo(true));
Expand Down Expand Up @@ -729,7 +742,9 @@ public void testIndicesGetAliases() throws Exception {
.removeAlias("foobar", "foo"));

getResponse = admin().indices().prepareGetAliases("foo").addIndices("foobar").get();
assertThat(getResponse.getAliases().isEmpty(), equalTo(true));
for (final ObjectObjectCursor<String, List<AliasMetaData>> entry : getResponse.getAliases()) {
assertTrue(entry.value.isEmpty());
}
existsResponse = admin().indices().prepareAliasesExist("foo").addIndices("foobar").get();
assertThat(existsResponse.exists(), equalTo(false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ setup:

---
"check delete with index list":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x

- do:
indices.delete_alias:
index: "test_index1,test_index2"
Expand All @@ -106,6 +110,10 @@ setup:

---
"check delete with prefix* index":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x

- do:
indices.delete_alias:
index: "test_*"
Expand All @@ -129,6 +137,10 @@ setup:

---
"check delete with index list and * aliases":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x

- do:
indices.delete_alias:
index: "test_index1,test_index2"
Expand All @@ -152,6 +164,10 @@ setup:

---
"check delete with index list and _all aliases":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x

- do:
indices.delete_alias:
index: "test_index1,test_index2"
Expand All @@ -175,6 +191,10 @@ setup:

---
"check delete with index list and wildcard aliases":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x

- do:
indices.delete_alias:
index: "test_index1,test_index2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,62 @@ setup:
- match: {test_index.aliases.test_blias: {}}
- is_false: test_index_2

---
"Get aliases via /_all/_alias/":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x

- do:
indices.create:
index: myindex

- do:
indices.get_alias:
index: _all

- match: {test_index.aliases.test_alias: {}}
- match: {test_index.aliases.test_blias: {}}
- match: {test_index_2.aliases.test_alias: {}}
- match: {test_index_2.aliases.test_blias: {}}
- match: {myindex.aliases: {}}

---
"Get aliases via /*/_alias/":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x

- do:
indices.create:
index: myindex

- do:
indices.get_alias:
index: "*"

- match: {test_index.aliases.test_alias: {}}
- match: {test_index.aliases.test_blias: {}}
- match: {test_index_2.aliases.test_alias: {}}
- match: {test_index_2.aliases.test_blias: {}}
- match: {myindex.aliases: {}}

---
"Get and index with no aliases via /{index}/_alias/":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x

- do:
indices.create:
index: myindex

- do:
indices.get_alias:
index: myindex

- match: {myindex.aliases: {}}

---
"Get specific alias via /{index}/_alias/{name}":

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ setup:

---
"put alias per index":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x

- do:
indices.put_alias:
Expand Down Expand Up @@ -69,7 +72,9 @@ setup:

---
"put alias prefix* index":

- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x

- do:
indices.put_alias:
Expand All @@ -86,7 +91,9 @@ setup:

---
"put alias in list of indices":

- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x

- do:
indices.put_alias:
Expand Down