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
This commit is contained in:
Lee Hinman 2017-06-07 10:27:43 -06:00
parent ee0e921643
commit 5b2ab96364
7 changed files with 126 additions and 17 deletions

View File

@ -243,7 +243,8 @@ public class MetaData implements Iterable<IndexMetaData>, Diffable<MetaData>, To
* *
* @param aliases The names of the index aliases to find * @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. * @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) { public ImmutableOpenMap<String, List<AliasMetaData>> findAliases(final String[] aliases, String[] concreteIndices) {
assert aliases != null; assert aliases != null;
@ -273,8 +274,8 @@ public class MetaData implements Iterable<IndexMetaData>, Diffable<MetaData>, To
return o1.alias().compareTo(o2.alias()); return o1.alias().compareTo(o2.alias());
} }
}); });
mapBuilder.put(index, Collections.unmodifiableList(filteredValues));
} }
mapBuilder.put(index, Collections.unmodifiableList(filteredValues));
} }
return mapBuilder.build(); return mapBuilder.build();
} }

View File

@ -76,6 +76,7 @@ public class RestGetAliasesAction extends BaseRestHandler {
@Override @Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
final boolean namesProvided = request.hasParam("name");
final String[] aliases = request.paramAsStringArrayOrEmptyIfAll("name"); final String[] aliases = request.paramAsStringArrayOrEmptyIfAll("name");
final GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases); final GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases);
final String[] indices = Strings.splitStringByCommaToArray(request.param("index")); final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
@ -89,9 +90,13 @@ public class RestGetAliasesAction extends BaseRestHandler {
final ImmutableOpenMap<String, List<AliasMetaData>> aliasMap = response.getAliases(); final ImmutableOpenMap<String, List<AliasMetaData>> aliasMap = response.getAliases();
final Set<String> aliasNames = new HashSet<>(); 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) { for (final AliasMetaData aliasMetaData : cursor.value) {
aliasNames.add(aliasMetaData.alias()); aliasNames.add(aliasMetaData.alias());
if (namesProvided) {
indicesToDisplay.add(cursor.key);
}
} }
} }
@ -131,6 +136,7 @@ public class RestGetAliasesAction extends BaseRestHandler {
} }
for (final ObjectObjectCursor<String, List<AliasMetaData>> entry : response.getAliases()) { for (final ObjectObjectCursor<String, List<AliasMetaData>> entry : response.getAliases()) {
if (namesProvided == false || (namesProvided && indicesToDisplay.contains(entry.key))) {
builder.startObject(entry.key); builder.startObject(entry.key);
{ {
builder.startObject("aliases"); builder.startObject("aliases");
@ -144,6 +150,7 @@ public class RestGetAliasesAction extends BaseRestHandler {
builder.endObject(); builder.endObject();
} }
} }
}
builder.endObject(); builder.endObject();
return new BytesRestResponse(status, builder); return new BytesRestResponse(status, builder);
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.action.admin.indices.get; 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.alias.Alias;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest.Feature; import org.elasticsearch.action.admin.indices.get.GetIndexRequest.Feature;
import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.AliasMetaData;
@ -281,6 +282,8 @@ public class GetIndexIT extends ESIntegTestCase {
private void assertEmptyAliases(GetIndexResponse response) { private void assertEmptyAliases(GetIndexResponse response) {
assertThat(response.aliases(), notNullValue()); assertThat(response.aliases(), notNullValue());
assertThat(response.aliases().isEmpty(), equalTo(true)); for (final ObjectObjectCursor<String, List<AliasMetaData>> entry : response.getAliases()) {
assertTrue(entry.value.isEmpty());
}
} }
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.aliases; 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.Alias;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions;
import org.elasticsearch.action.admin.indices.alias.exists.AliasesExistResponse; import org.elasticsearch.action.admin.indices.alias.exists.AliasesExistResponse;
@ -32,6 +33,7 @@ import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.AliasOrIndex; import org.elasticsearch.cluster.metadata.AliasOrIndex;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.StopWatch; import org.elasticsearch.common.StopWatch;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
@ -49,6 +51,7 @@ import org.elasticsearch.test.ESIntegTestCase;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -567,20 +570,24 @@ public class IndexAliasesIT extends ESIntegTestCase {
logger.info("--> getting alias1"); logger.info("--> getting alias1");
GetAliasesResponse getResponse = admin().indices().prepareGetAliases("alias1").get(); GetAliasesResponse getResponse = admin().indices().prepareGetAliases("alias1").get();
assertThat(getResponse, notNullValue()); 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").size(), equalTo(1));
assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); 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).alias(), equalTo("alias1"));
assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue()); 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).getIndexRouting(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), 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(); AliasesExistResponse existsResponse = admin().indices().prepareAliasesExist("alias1").get();
assertThat(existsResponse.exists(), equalTo(true)); assertThat(existsResponse.exists(), equalTo(true));
logger.info("--> getting all aliases that start with alias*"); logger.info("--> getting all aliases that start with alias*");
getResponse = admin().indices().prepareGetAliases("alias*").get(); getResponse = admin().indices().prepareGetAliases("alias*").get();
assertThat(getResponse, notNullValue()); 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").size(), equalTo(2));
assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); 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).alias(), equalTo("alias1"));
@ -592,6 +599,10 @@ public class IndexAliasesIT extends ESIntegTestCase {
assertThat(getResponse.getAliases().get("foobar").get(1).getFilter(), nullValue()); 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).getIndexRouting(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(1).getSearchRouting(), 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(); existsResponse = admin().indices().prepareAliasesExist("alias*").get();
assertThat(existsResponse.exists(), equalTo(true)); assertThat(existsResponse.exists(), equalTo(true));
@ -676,12 +687,13 @@ public class IndexAliasesIT extends ESIntegTestCase {
logger.info("--> getting f* for index *bar"); logger.info("--> getting f* for index *bar");
getResponse = admin().indices().prepareGetAliases("f*").addIndices("*bar").get(); getResponse = admin().indices().prepareGetAliases("f*").addIndices("*bar").get();
assertThat(getResponse, notNullValue()); 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), notNullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo")); 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).getFilter(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
assertTrue(getResponse.getAliases().get("bazbar").isEmpty());
existsResponse = admin().indices().prepareAliasesExist("f*") existsResponse = admin().indices().prepareAliasesExist("f*")
.addIndices("*bar").get(); .addIndices("*bar").get();
assertThat(existsResponse.exists(), equalTo(true)); assertThat(existsResponse.exists(), equalTo(true));
@ -690,13 +702,14 @@ public class IndexAliasesIT extends ESIntegTestCase {
logger.info("--> getting f* for index *bac"); logger.info("--> getting f* for index *bac");
getResponse = admin().indices().prepareGetAliases("foo").addIndices("*bac").get(); getResponse = admin().indices().prepareGetAliases("foo").addIndices("*bac").get();
assertThat(getResponse, notNullValue()); 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").size(), equalTo(1));
assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue()); 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).alias(), equalTo("foo"));
assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue()); 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).getIndexRouting(), nullValue());
assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue()); assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
assertTrue(getResponse.getAliases().get("bazbar").isEmpty());
existsResponse = admin().indices().prepareAliasesExist("foo") existsResponse = admin().indices().prepareAliasesExist("foo")
.addIndices("*bac").get(); .addIndices("*bac").get();
assertThat(existsResponse.exists(), equalTo(true)); assertThat(existsResponse.exists(), equalTo(true));
@ -729,7 +742,9 @@ public class IndexAliasesIT extends ESIntegTestCase {
.removeAlias("foobar", "foo")); .removeAlias("foobar", "foo"));
getResponse = admin().indices().prepareGetAliases("foo").addIndices("foobar").get(); 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(); existsResponse = admin().indices().prepareAliasesExist("foo").addIndices("foobar").get();
assertThat(existsResponse.exists(), equalTo(false)); assertThat(existsResponse.exists(), equalTo(false));
} }

View File

@ -84,6 +84,10 @@ setup:
--- ---
"check delete with index list": "check delete with index list":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x
- do: - do:
indices.delete_alias: indices.delete_alias:
index: "test_index1,test_index2" index: "test_index1,test_index2"
@ -106,6 +110,10 @@ setup:
--- ---
"check delete with prefix* index": "check delete with prefix* index":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x
- do: - do:
indices.delete_alias: indices.delete_alias:
index: "test_*" index: "test_*"
@ -129,6 +137,10 @@ setup:
--- ---
"check delete with index list and * aliases": "check delete with index list and * aliases":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x
- do: - do:
indices.delete_alias: indices.delete_alias:
index: "test_index1,test_index2" index: "test_index1,test_index2"
@ -152,6 +164,10 @@ setup:
--- ---
"check delete with index list and _all aliases": "check delete with index list and _all aliases":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x
- do: - do:
indices.delete_alias: indices.delete_alias:
index: "test_index1,test_index2" index: "test_index1,test_index2"
@ -175,6 +191,10 @@ setup:
--- ---
"check delete with index list and wildcard aliases": "check delete with index list and wildcard aliases":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x
- do: - do:
indices.delete_alias: indices.delete_alias:
index: "test_index1,test_index2" index: "test_index1,test_index2"

View File

@ -40,6 +40,62 @@ setup:
- match: {test_index.aliases.test_blias: {}} - match: {test_index.aliases.test_blias: {}}
- is_false: test_index_2 - 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}": "Get specific alias via /{index}/_alias/{name}":

View File

@ -14,6 +14,9 @@ setup:
--- ---
"put alias per index": "put alias per index":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x
- do: - do:
indices.put_alias: indices.put_alias:
@ -69,7 +72,9 @@ setup:
--- ---
"put alias prefix* index": "put alias prefix* index":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x
- do: - do:
indices.put_alias: indices.put_alias:
@ -86,7 +91,9 @@ setup:
--- ---
"put alias in list of indices": "put alias in list of indices":
- skip:
version: " - 5.99.99"
reason: only requested indices are included in 6.x
- do: - do:
indices.put_alias: indices.put_alias: