Skip to content

Commit

Permalink
Add docs for errors in GetAlias API (elastic#51850) (elastic#52716)
Browse files Browse the repository at this point in the history
Closes elastic#31499

Co-authored-by: Maxim <[email protected]>
  • Loading branch information
probakowski and timoninmaxim authored Feb 24, 2020
1 parent f993ef8 commit e72cb79
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1382,21 +1382,25 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException {
highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getError(), equalTo("alias [" + alias + "] missing"));
assertThat(getAliasesResponse.getException(), nullValue());
}
createIndex(index, Settings.EMPTY);
client().performRequest(new Request(HttpPut.METHOD_NAME, index + "/_alias/" + alias));
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index");
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias,
highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.getAliases().size(), equalTo(0));
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getError(), nullValue());
assertThat(getAliasesResponse.getException().getMessage(),
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
}
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index").aliases(alias);
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias,
highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.getAliases().size(), equalTo(0));
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getException().getMessage(),
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
Expand All @@ -1405,13 +1409,17 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException {
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices("non_existent_index*");
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias,
highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.status(), equalTo(RestStatus.OK));
assertThat(getAliasesResponse.getAliases().size(), equalTo(0));
assertThat(getAliasesResponse.getException(), nullValue());
assertThat(getAliasesResponse.getError(), nullValue());
}
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index).aliases(alias, "non_existent_alias");
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias,
highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getError(), equalTo("alias [non_existent_alias] missing"));

assertThat(getAliasesResponse.getAliases().size(), equalTo(1));
assertThat(getAliasesResponse.getAliases().get(index).size(), equalTo(1));
Expand All @@ -1431,6 +1439,13 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException {
}
*/
}
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().aliases("non_existent_alias*");
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias,
highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.status(), equalTo(RestStatus.OK));
assertThat(getAliasesResponse.getAliases().size(), equalTo(0));
}
}

public void testIndexPutSettings() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;

/**
* This class is used to generate the Java Indices API documentation.
Expand Down Expand Up @@ -2000,8 +2001,17 @@ public void testGetAlias() throws Exception {
Map<String, Set<AliasMetaData>> aliases = response.getAliases(); // <1>
// end::get-alias-response

// tag::get-alias-response-error
RestStatus status = response.status(); // <1>
ElasticsearchException exception = response.getException(); // <2>
String error = response.getError(); // <3>
// end::get-alias-response-error

assertThat(response.getAliases().get("index").size(), equalTo(1));
assertThat(response.getAliases().get("index").iterator().next().alias(), equalTo("alias"));
assertThat(status, equalTo(RestStatus.OK));
assertThat(error, nullValue());
assertThat(exception, nullValue());

// tag::get-alias-execute-listener
ActionListener<GetAliasesResponse> listener =
Expand Down
22 changes: 21 additions & 1 deletion docs/java-rest/high-level/indices/get_alias.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,24 @@ executed operation as follows:
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-response]
--------------------------------------------------
<1> Retrieves a map of indices and their aliases
<1> Retrieves a map of indices and their aliases

+{response}+ class contains information about errors if they occurred.
This info could be in fields `error` or `exception` depends on a case.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-response-error]
--------------------------------------------------
<1> Client sets status to `NOT_FOUND` if at least one item of specified
indices or aliases is not found. Otherwise it is `OK`.

<2> If at least one item of specified indices isn't exist client sets
`ElasticsearchException` and returns empty result.

<3> If at least one item of specified aliases ins't exist client puts
error description in `error` field and returns partial result if any
of other patterns match.

If user specified indices or aliases as regular expressions
and nothing was found client returns `OK` status and no errors.

0 comments on commit e72cb79

Please sign in to comment.