From 38dbb8da9582f201e24c70def2a1d2a2058c52bd Mon Sep 17 00:00:00 2001 From: javanna Date: Fri, 19 Jan 2018 11:52:53 +0100 Subject: [PATCH 1/5] REST high-level client: add support for exists alias --- .../elasticsearch/client/IndicesClient.java | 25 +++++++ .../org/elasticsearch/client/Request.java | 44 ++++++++++-- .../elasticsearch/client/IndicesClientIT.java | 17 +++++ .../elasticsearch/client/RequestTests.java | 57 +++++++++++++-- .../IndicesClientDocumentationIT.java | 56 ++++++++++++--- .../high-level/apis/exists_alias.asciidoc | 69 +++++++++++++++++++ docs/java-rest/high-level/apis/index.asciidoc | 2 + .../high-level/supported-apis.asciidoc | 1 + 8 files changed, 251 insertions(+), 20 deletions(-) create mode 100644 docs/java-rest/high-level/apis/exists_alias.asciidoc diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java index 29a754035aaae..3b5b2818fcad9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java @@ -21,6 +21,7 @@ import org.apache.http.Header; import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; import org.elasticsearch.action.admin.indices.close.CloseIndexResponse; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; @@ -37,6 +38,8 @@ import java.io.IOException; import java.util.Collections; +import static java.util.Collections.emptySet; + /** * A wrapper for the {@link RestHighLevelClient} that provides methods for accessing the Indices API. *

@@ -184,4 +187,26 @@ public void closeAsync(CloseIndexRequest closeIndexRequest, ActionListener + * See + * Indices Aliases API on elastic.co + */ + public boolean existsAlias(GetAliasesRequest getAliasesRequest, Header... headers) throws IOException { + return restHighLevelClient.performRequest(getAliasesRequest, Request::existsAlias, RestHighLevelClient::convertExistsResponse, + emptySet(), headers); + } + + /** + * Asynchronously checks if one or more aliases exist using the Aliases Exist API + *

+ * See + * Indices Aliases API on elastic.co + */ + public void existsAliasAsync(GetAliasesRequest getAliasesRequest, ActionListener listener, Header... headers) { + restHighLevelClient.performRequestAsync(getAliasesRequest, Request::existsAlias, RestHighLevelClient::convertExistsResponse, + listener, emptySet(), headers); + } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java index b971465cb8853..f28149425d7f4 100755 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java @@ -29,6 +29,7 @@ import org.apache.http.entity.ContentType; import org.apache.lucene.util.BytesRef; import org.elasticsearch.action.DocWriteRequest; +import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; @@ -144,7 +145,7 @@ static Request deleteIndex(DeleteIndexRequest deleteIndexRequest) { } static Request openIndex(OpenIndexRequest openIndexRequest) { - String endpoint = endpoint(openIndexRequest.indices(), Strings.EMPTY_ARRAY, "_open"); + String endpoint = endpoint(openIndexRequest.indices(), "_open"); Params parameters = Params.builder(); @@ -157,7 +158,7 @@ static Request openIndex(OpenIndexRequest openIndexRequest) { } static Request closeIndex(CloseIndexRequest closeIndexRequest) { - String endpoint = endpoint(closeIndexRequest.indices(), Strings.EMPTY_ARRAY, "_close"); + String endpoint = endpoint(closeIndexRequest.indices(), "_close"); Params parameters = Params.builder(); @@ -472,13 +473,41 @@ static Request multiSearch(MultiSearchRequest multiSearchRequest) throws IOExcep return new Request(HttpPost.METHOD_NAME, "/_msearch", params.getParams(), entity); } + static Request existsAlias(GetAliasesRequest getAliasesRequest) { + Params params = Params.builder(); + params.withIndicesOptions(getAliasesRequest.indicesOptions()); + params.withLocal(getAliasesRequest.local()); + String endpoint = endpoint(getAliasesRequest.indices(), "_alias", getAliasesRequest.aliases()); + return new Request("HEAD", endpoint, params.getParams(), null); + } + private static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException { BytesRef source = XContentHelper.toXContent(toXContent, xContentType, false).toBytesRef(); return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(xContentType)); } + static String endpoint(String index, String type, String id) { + return buildEndpoint(index, type, id); + } + + static String endpoint(String index, String type, String id, String endpoint) { + return buildEndpoint(index, type, id, endpoint); + } + + static String endpoint(String[] indices) { + return buildEndpoint(String.join(",", indices)); + } + + static String endpoint(String[] indices, String endpoint) { + return buildEndpoint(String.join(",", indices), endpoint); + } + static String endpoint(String[] indices, String[] types, String endpoint) { - return endpoint(String.join(",", indices), String.join(",", types), endpoint); + return buildEndpoint(String.join(",", indices), String.join(",", types), endpoint); + } + + static String endpoint(String[] indices, String endpoint, String[] suffixes) { + return buildEndpoint(String.join(",", indices), endpoint, String.join(",", suffixes)); } static String endpoint(String[] indices, String endpoint, String type) { @@ -486,9 +515,9 @@ static String endpoint(String[] indices, String endpoint, String type) { } /** - * Utility method to build request's endpoint. + * Utility method to build request's endpoint given its parts as strings */ - static String endpoint(String... parts) { + static String buildEndpoint(String... parts) { StringJoiner joiner = new StringJoiner("/", "/", ""); for (String part : parts) { if (Strings.hasLength(part)) { @@ -656,6 +685,11 @@ Params withIndicesOptions(IndicesOptions indicesOptions) { return this; } + Params withLocal(boolean local) { + putParam("local", Boolean.toString(local)); + return this; + } + Map getParams() { return Collections.unmodifiableMap(params); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 58569b687c780..0ec00a2ed8e6e 100755 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -22,6 +22,7 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; import org.elasticsearch.action.admin.indices.close.CloseIndexResponse; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; @@ -353,6 +354,22 @@ private static boolean aliasExists(String index, String alias) throws IOExceptio return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode(); } + public void testExistsAlias() throws IOException { + GetAliasesRequest getAliasesRequest = new GetAliasesRequest("alias"); + assertFalse(execute(getAliasesRequest, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync)); + + client().performRequest("PUT", "/index"); + client().performRequest("PUT", "/index/_alias/alias"); + assertTrue(execute(getAliasesRequest, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync)); + + GetAliasesRequest getAliasesRequest2 = new GetAliasesRequest(); + getAliasesRequest2.aliases("alias"); + getAliasesRequest2.indices("index"); + assertTrue(execute(getAliasesRequest2, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync)); + getAliasesRequest2.indices("does_not_exist"); + assertFalse(execute(getAliasesRequest2, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync)); + } + @SuppressWarnings("unchecked") private Map getIndexMetadata(String index) throws IOException { Response response = client().performRequest("GET", index); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java index 8282e29c1ec50..470aa478f835c 100755 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java @@ -30,6 +30,7 @@ import org.apache.http.entity.StringEntity; import org.apache.http.util.EntityUtils; import org.elasticsearch.action.DocWriteRequest; +import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions; @@ -983,6 +984,37 @@ public void testClearScroll() throws IOException { assertEquals(REQUEST_BODY_CONTENT_TYPE.mediaTypeWithoutParameters(), request.getEntity().getContentType().getValue()); } + public void testExistsAlias() { + GetAliasesRequest getAliasesRequest = new GetAliasesRequest(); + String[] indices = randomIndicesNames(0, 5); + getAliasesRequest.indices(indices); + String[] aliases = randomIndicesNames(0, 5); + getAliasesRequest.aliases(aliases); + Map expectedParams = new HashMap<>(); + if (randomBoolean()) { + boolean local = randomBoolean(); + getAliasesRequest.local(local); + } + expectedParams.put("local", Boolean.toString(getAliasesRequest.local())); + + setRandomIndicesOptions(getAliasesRequest::indicesOptions, getAliasesRequest::indicesOptions, expectedParams); + + Request request = Request.existsAlias(getAliasesRequest); + StringJoiner endpoint = new StringJoiner("/", "/", ""); + String index = String.join(",", indices); + if (Strings.hasLength(index)) { + endpoint.add(index); + } + endpoint.add("_alias"); + String alias = String.join(",", aliases); + if (Strings.hasLength(alias)) { + endpoint.add(alias); + } + assertEquals(endpoint.toString(), request.getEndpoint()); + assertEquals(expectedParams, request.getParameters()); + assertNull(request.getEntity()); + } + private static void assertToXContentBody(ToXContent expectedBody, HttpEntity actualEntity) throws IOException { BytesReference expectedBytes = XContentHelper.toXContent(expectedBody, REQUEST_BODY_CONTENT_TYPE, false); assertEquals(XContentType.JSON.mediaTypeWithoutParameters(), actualEntity.getContentType().getValue()); @@ -1017,14 +1049,25 @@ public void testParamsNoDuplicates() { assertEquals("1", requestParams.values().iterator().next()); } + public void testBuildEndpoint() { + assertEquals("/", Request.buildEndpoint()); + assertEquals("/", Request.buildEndpoint(Strings.EMPTY_ARRAY)); + assertEquals("/", Request.buildEndpoint("")); + assertEquals("/a/b", Request.buildEndpoint("a", "b")); + assertEquals("/a/b/_create", Request.buildEndpoint("a", "b", "_create")); + assertEquals("/a/b/c/_create", Request.buildEndpoint("a", "b", "c", "_create")); + assertEquals("/a/_create", Request.buildEndpoint("a", null, null, "_create")); + } + public void testEndpoint() { - assertEquals("/", Request.endpoint()); - assertEquals("/", Request.endpoint(Strings.EMPTY_ARRAY)); - assertEquals("/", Request.endpoint("")); - assertEquals("/a/b", Request.endpoint("a", "b")); - assertEquals("/a/b/_create", Request.endpoint("a", "b", "_create")); - assertEquals("/a/b/c/_create", Request.endpoint("a", "b", "c", "_create")); - assertEquals("/a/_create", Request.endpoint("a", null, null, "_create")); + assertEquals("/index/type/id", Request.endpoint("index", "type", "id")); + assertEquals("/index/type/id/_endpoint", Request.endpoint("index", "type", "id", "_endpoint")); + assertEquals("/index1,index2", Request.endpoint(new String[]{"index1", "index2"})); + assertEquals("/index1,index2/_endpoint", Request.endpoint(new String[]{"index1", "index2"}, "_endpoint")); + assertEquals("/index1,index2/type1,type2/_endpoint", Request.endpoint(new String[]{"index1", "index2"}, + new String[]{"type1", "type2"}, "_endpoint")); + assertEquals("/index1,index2/_endpoint/suffix1,suffix2", Request.endpoint(new String[]{"index1", "index2"}, + "_endpoint", new String[]{"suffix1", "suffix2"})); } public void testCreateContentType() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java index 710c3fe1d7c16..1b9467c0c870c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java @@ -22,6 +22,7 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; import org.elasticsearch.action.admin.indices.close.CloseIndexResponse; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; @@ -433,18 +434,57 @@ public void onFailure(Exception e) { }); // end::close-index-execute-async } + } + + public void testExistsAlias() throws IOException { + RestHighLevelClient client = highLevelClient(); { - // tag::close-index-notfound - try { - CloseIndexRequest request = new CloseIndexRequest("does_not_exist"); - client.indices().close(request); - } catch (ElasticsearchException exception) { - if (exception.status() == RestStatus.BAD_REQUEST) { + CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index") + .alias(new Alias("alias"))); + assertTrue(createIndexResponse.isAcknowledged()); + } + + { + // tag::exists-alias-request + GetAliasesRequest request = new GetAliasesRequest(); + GetAliasesRequest requestWithAlias = new GetAliasesRequest("alias1"); + GetAliasesRequest requestWithAliases = new GetAliasesRequest(new String[]{"alias1", "alias2"}); + // end::exists-alias-request + + // tag::exists-alias-request-alias + request.aliases("alias"); // <1> + // end::exists-alias-request-alias + // tag::exists-alias-request-indices + request.indices("index"); // <1> + // end::exists-alias-request-indices + + // tag::exists-alias-request-indicesOptions + request.indicesOptions(IndicesOptions.lenientExpandOpen()); // <1> + // end::exists-alias-request-indicesOptions + + // tag::exists-alias-request-local + request.local(true); // <1> + // end::exists-alias-request-local + + // tag::exists-alias-execute + boolean exists = client.indices().existsAlias(request); + // end::exists-alias-execute + assertTrue(exists); + + // tag::exists-alias-execute-async + client.indices().existsAliasAsync(request, new ActionListener() { + @Override + public void onResponse(Boolean exists) { // <1> } - } - // end::close-index-notfound + + @Override + public void onFailure(Exception e) { + // <2> + } + }); + // end::exists-alias-execute-async } } diff --git a/docs/java-rest/high-level/apis/exists_alias.asciidoc b/docs/java-rest/high-level/apis/exists_alias.asciidoc new file mode 100644 index 0000000000000..a7ce74a21ea67 --- /dev/null +++ b/docs/java-rest/high-level/apis/exists_alias.asciidoc @@ -0,0 +1,69 @@ +[[java-rest-high-exists-alias]] +=== Exists Alias API + +[[java-rest-high-exists-alias-request]] +==== Exists Alias Request + +The Exists Alias API uses `GetAliasesRequest` as its request object. +One or more aliases can be optionally provided either at construction +time or later on through the relevant setter method. + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[exists-alias-request] +-------------------------------------------------- + +==== Optional arguments +The following arguments can optionally be provided: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[exists-alias-request-alias] +-------------------------------------------------- +<1> One or more aliases to look for + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[exists-alias-request-indices] +-------------------------------------------------- +<1> The index or indices that the alias is associated with + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[exists-alias-request-indicesOptions] +-------------------------------------------------- +<1> Setting `IndicesOptions` controls how unavailable indices are resolved and +how wildcard expressions are expanded + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[exists-alias-request-local] +-------------------------------------------------- +<1> The `local` flag (defaults to `false`) controls whether the aliases need +to be looked up in the local cluster state or in the cluster state held by +the elected master node. + +[[java-rest-high-exists-alias-sync]] +==== Synchronous Execution + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[exists-alias-execute] +-------------------------------------------------- + +[[java-rest-high-exists-alias-async]] +==== Asynchronous Execution + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[exists-alias-execute-async] +-------------------------------------------------- +<1> Called when the execution is successfully completed. The response is +provided as an argument +<2> Called in case of failure. The raised exception is provided as an argument + +[[java-rest-high-exists-alias-response]] +==== Exists Alias Response + +The Exists Alias API returns a `boolean` that indicates whether the provided +alias (or aliases) were found or not. diff --git a/docs/java-rest/high-level/apis/index.asciidoc b/docs/java-rest/high-level/apis/index.asciidoc index b24d226671179..5f1f15b644799 100644 --- a/docs/java-rest/high-level/apis/index.asciidoc +++ b/docs/java-rest/high-level/apis/index.asciidoc @@ -10,6 +10,8 @@ include::putmapping.asciidoc[] include::update_aliases.asciidoc[] +include::exists_alias.asciidoc[] + include::_index.asciidoc[] include::get.asciidoc[] diff --git a/docs/java-rest/high-level/supported-apis.asciidoc b/docs/java-rest/high-level/supported-apis.asciidoc index 4fe32401cac31..8eb2590e99d9d 100644 --- a/docs/java-rest/high-level/supported-apis.asciidoc +++ b/docs/java-rest/high-level/supported-apis.asciidoc @@ -10,6 +10,7 @@ Indices APIs:: * <> * <> * <> +* <> Single document APIs:: * <> From 5cb6e7d935a6baabb3017b8450110783caffa7b7 Mon Sep 17 00:00:00 2001 From: javanna Date: Tue, 23 Jan 2018 17:50:57 +0100 Subject: [PATCH 2/5] address review comments --- .../elasticsearch/client/IndicesClient.java | 25 +++++++++---------- .../org/elasticsearch/client/Request.java | 4 +-- .../elasticsearch/client/IndicesClientIT.java | 2 +- .../IndicesClientDocumentationIT.java | 24 +++++++++++++++--- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java index 3b5b2818fcad9..661edbbeab1cd 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java @@ -36,7 +36,6 @@ import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; import java.io.IOException; -import java.util.Collections; import static java.util.Collections.emptySet; @@ -60,7 +59,7 @@ public final class IndicesClient { */ public DeleteIndexResponse delete(DeleteIndexRequest deleteIndexRequest, Header... headers) throws IOException { return restHighLevelClient.performRequestAndParseEntity(deleteIndexRequest, Request::deleteIndex, DeleteIndexResponse::fromXContent, - Collections.emptySet(), headers); + emptySet(), headers); } /** @@ -71,7 +70,7 @@ public DeleteIndexResponse delete(DeleteIndexRequest deleteIndexRequest, Header. */ public void deleteAsync(DeleteIndexRequest deleteIndexRequest, ActionListener listener, Header... headers) { restHighLevelClient.performRequestAsyncAndParseEntity(deleteIndexRequest, Request::deleteIndex, DeleteIndexResponse::fromXContent, - listener, Collections.emptySet(), headers); + listener, emptySet(), headers); } /** @@ -82,7 +81,7 @@ public void deleteAsync(DeleteIndexRequest deleteIndexRequest, ActionListener listener, Header... headers) { restHighLevelClient.performRequestAsyncAndParseEntity(createIndexRequest, Request::createIndex, CreateIndexResponse::fromXContent, - listener, Collections.emptySet(), headers); + listener, emptySet(), headers); } /** @@ -104,7 +103,7 @@ public void createAsync(CreateIndexRequest createIndexRequest, ActionListener listener, Header... headers) { restHighLevelClient.performRequestAsyncAndParseEntity(putMappingRequest, Request::putMapping, PutMappingResponse::fromXContent, - listener, Collections.emptySet(), headers); + listener, emptySet(), headers); } /** @@ -128,7 +127,7 @@ public void putMappingAsync(PutMappingRequest putMappingRequest, ActionListener< */ public IndicesAliasesResponse updateAliases(IndicesAliasesRequest indicesAliasesRequest, Header... headers) throws IOException { return restHighLevelClient.performRequestAndParseEntity(indicesAliasesRequest, Request::updateAliases, - IndicesAliasesResponse::fromXContent, Collections.emptySet(), headers); + IndicesAliasesResponse::fromXContent, emptySet(), headers); } /** @@ -141,7 +140,7 @@ public IndicesAliasesResponse updateAliases(IndicesAliasesRequest indicesAliases public void updateAliasesAsync(IndicesAliasesRequest indicesAliasesRequestRequest, ActionListener listener, Header... headers) { restHighLevelClient.performRequestAsyncAndParseEntity(indicesAliasesRequestRequest, Request::updateAliases, - IndicesAliasesResponse::fromXContent, listener, Collections.emptySet(), headers); + IndicesAliasesResponse::fromXContent, listener, emptySet(), headers); } /** @@ -152,7 +151,7 @@ public void updateAliasesAsync(IndicesAliasesRequest indicesAliasesRequestReques */ public OpenIndexResponse open(OpenIndexRequest openIndexRequest, Header... headers) throws IOException { return restHighLevelClient.performRequestAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent, - Collections.emptySet(), headers); + emptySet(), headers); } /** @@ -163,7 +162,7 @@ public OpenIndexResponse open(OpenIndexRequest openIndexRequest, Header... heade */ public void openAsync(OpenIndexRequest openIndexRequest, ActionListener listener, Header... headers) { restHighLevelClient.performRequestAsyncAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent, - listener, Collections.emptySet(), headers); + listener, emptySet(), headers); } /** @@ -174,7 +173,7 @@ public void openAsync(OpenIndexRequest openIndexRequest, ActionListener listener, Header... headers) { restHighLevelClient.performRequestAsyncAndParseEntity(closeIndexRequest, Request::closeIndex, CloseIndexResponse::fromXContent, - listener, Collections.emptySet(), headers); + listener, emptySet(), headers); } /** diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java index f28149425d7f4..1b059ca5cba04 100755 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java @@ -134,7 +134,7 @@ static Request delete(DeleteRequest deleteRequest) { } static Request deleteIndex(DeleteIndexRequest deleteIndexRequest) { - String endpoint = endpoint(deleteIndexRequest.indices(), Strings.EMPTY_ARRAY, ""); + String endpoint = endpoint(deleteIndexRequest.indices()); Params parameters = Params.builder(); parameters.withTimeout(deleteIndexRequest.timeout()); @@ -170,7 +170,7 @@ static Request closeIndex(CloseIndexRequest closeIndexRequest) { } static Request createIndex(CreateIndexRequest createIndexRequest) throws IOException { - String endpoint = endpoint(createIndexRequest.indices(), Strings.EMPTY_ARRAY, ""); + String endpoint = endpoint(createIndexRequest.indices()); Params parameters = Params.builder(); parameters.withTimeout(createIndexRequest.timeout()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 0ec00a2ed8e6e..6fb9bd4c0fd35 100755 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -358,7 +358,7 @@ public void testExistsAlias() throws IOException { GetAliasesRequest getAliasesRequest = new GetAliasesRequest("alias"); assertFalse(execute(getAliasesRequest, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync)); - client().performRequest("PUT", "/index"); + createIndex("index"); client().performRequest("PUT", "/index/_alias/alias"); assertTrue(execute(getAliasesRequest, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java index 1b9467c0c870c..5f125a0cd405b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java @@ -314,7 +314,7 @@ public void onFailure(Exception e) { } } - public void testOpenIndex() throws IOException { + public void testOpenIndex() throws Exception { RestHighLevelClient client = highLevelClient(); { @@ -383,9 +383,15 @@ public void onFailure(Exception e) { } // end::open-index-notfound } + + assertBusy(() -> { + // TODO Use Indices Exist API instead once it exists + Response response = client.getLowLevelClient().performRequest("HEAD", "index"); + assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode()); + }); } - public void testCloseIndex() throws IOException { + public void testCloseIndex() throws Exception { RestHighLevelClient client = highLevelClient(); { @@ -433,10 +439,16 @@ public void onFailure(Exception e) { } }); // end::close-index-execute-async + + assertBusy(() -> { + // TODO Use Indices Exist API instead once it exists + Response response = client.getLowLevelClient().performRequest("HEAD", "index"); + assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode()); + }); } } - public void testExistsAlias() throws IOException { + public void testExistsAlias() throws Exception { RestHighLevelClient client = highLevelClient(); { @@ -485,6 +497,12 @@ public void onFailure(Exception e) { } }); // end::exists-alias-execute-async + + assertBusy(() -> { + // TODO Use Indices Exist API instead once it exists + Response response = client.getLowLevelClient().performRequest("HEAD", "index"); + assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode()); + }); } } From dcf0994b377992a074417e66a3b5307fe55e8ea9 Mon Sep 17 00:00:00 2001 From: javanna Date: Wed, 24 Jan 2018 11:09:04 +0100 Subject: [PATCH 3/5] remove assertBusy blocks --- .../IndicesClientDocumentationIT.java | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java index 5f125a0cd405b..1bcfb0bbc24a0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java @@ -305,12 +305,6 @@ public void onFailure(Exception e) { } }); // end::put-mapping-execute-async - - assertBusy(() -> { - // TODO Use Indices Exist API instead once it exists - Response response = client.getLowLevelClient().performRequest("HEAD", "twitter"); - assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode()); - }); } } @@ -383,12 +377,6 @@ public void onFailure(Exception e) { } // end::open-index-notfound } - - assertBusy(() -> { - // TODO Use Indices Exist API instead once it exists - Response response = client.getLowLevelClient().performRequest("HEAD", "index"); - assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode()); - }); } public void testCloseIndex() throws Exception { @@ -440,11 +428,6 @@ public void onFailure(Exception e) { }); // end::close-index-execute-async - assertBusy(() -> { - // TODO Use Indices Exist API instead once it exists - Response response = client.getLowLevelClient().performRequest("HEAD", "index"); - assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode()); - }); } } @@ -497,12 +480,6 @@ public void onFailure(Exception e) { } }); // end::exists-alias-execute-async - - assertBusy(() -> { - // TODO Use Indices Exist API instead once it exists - Response response = client.getLowLevelClient().performRequest("HEAD", "index"); - assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode()); - }); } } From 55e87d8deeae16f57dbaafe2abcbecb04e8b74e6 Mon Sep 17 00:00:00 2001 From: javanna Date: Wed, 24 Jan 2018 11:10:50 +0100 Subject: [PATCH 4/5] address review comments --- .../java/org/elasticsearch/client/RequestTests.java | 10 +++++----- docs/java-rest/high-level/apis/exists_alias.asciidoc | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java index 470aa478f835c..120129b5375d3 100755 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java @@ -1000,17 +1000,17 @@ public void testExistsAlias() { setRandomIndicesOptions(getAliasesRequest::indicesOptions, getAliasesRequest::indicesOptions, expectedParams); Request request = Request.existsAlias(getAliasesRequest); - StringJoiner endpoint = new StringJoiner("/", "/", ""); + StringJoiner expectedEndpoint = new StringJoiner("/", "/", ""); String index = String.join(",", indices); if (Strings.hasLength(index)) { - endpoint.add(index); + expectedEndpoint.add(index); } - endpoint.add("_alias"); + expectedEndpoint.add("_alias"); String alias = String.join(",", aliases); if (Strings.hasLength(alias)) { - endpoint.add(alias); + expectedEndpoint.add(alias); } - assertEquals(endpoint.toString(), request.getEndpoint()); + assertEquals(expectedEndpoint.toString(), request.getEndpoint()); assertEquals(expectedParams, request.getParameters()); assertNull(request.getEntity()); } diff --git a/docs/java-rest/high-level/apis/exists_alias.asciidoc b/docs/java-rest/high-level/apis/exists_alias.asciidoc index a7ce74a21ea67..cbbb2c3315c2b 100644 --- a/docs/java-rest/high-level/apis/exists_alias.asciidoc +++ b/docs/java-rest/high-level/apis/exists_alias.asciidoc @@ -20,7 +20,7 @@ The following arguments can optionally be provided: -------------------------------------------------- include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[exists-alias-request-alias] -------------------------------------------------- -<1> One or more aliases to look for +<1> One or more aliases to look for ["source","java",subs="attributes,callouts,macros"] -------------------------------------------------- @@ -66,4 +66,4 @@ provided as an argument ==== Exists Alias Response The Exists Alias API returns a `boolean` that indicates whether the provided -alias (or aliases) were found or not. +alias (or aliases) was found or not. From 0f32d21eb78e15ce3fbe59e9c45568f6e7c9a7db Mon Sep 17 00:00:00 2001 From: javanna Date: Thu, 25 Jan 2018 11:25:04 +0100 Subject: [PATCH 5/5] add protection for existsAlias with no index and no alias --- .../src/main/java/org/elasticsearch/client/Request.java | 3 +++ .../test/java/org/elasticsearch/client/RequestTests.java | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java index 1b059ca5cba04..04c10f28b2e0b 100755 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java @@ -477,6 +477,9 @@ static Request existsAlias(GetAliasesRequest getAliasesRequest) { Params params = Params.builder(); params.withIndicesOptions(getAliasesRequest.indicesOptions()); params.withLocal(getAliasesRequest.local()); + if (getAliasesRequest.indices().length == 0 && getAliasesRequest.aliases().length == 0) { + throw new IllegalArgumentException("existsAlias requires at least an alias or an index"); + } String endpoint = endpoint(getAliasesRequest.indices(), "_alias", getAliasesRequest.aliases()); return new Request("HEAD", endpoint, params.getParams(), null); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java index 120129b5375d3..e76833f84a0d7 100755 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java @@ -988,7 +988,8 @@ public void testExistsAlias() { GetAliasesRequest getAliasesRequest = new GetAliasesRequest(); String[] indices = randomIndicesNames(0, 5); getAliasesRequest.indices(indices); - String[] aliases = randomIndicesNames(0, 5); + //the HEAD endpoint requires at least an alias or an index + String[] aliases = randomIndicesNames(indices.length == 0 ? 1 : 0, 5); getAliasesRequest.aliases(aliases); Map expectedParams = new HashMap<>(); if (randomBoolean()) { @@ -1015,6 +1016,12 @@ public void testExistsAlias() { assertNull(request.getEntity()); } + public void testExistsAliasNoAliasNoIndex() { + GetAliasesRequest getAliasesRequest = new GetAliasesRequest(); + IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> Request.existsAlias(getAliasesRequest)); + assertEquals("existsAlias requires at least an alias or an index", iae.getMessage()); + } + private static void assertToXContentBody(ToXContent expectedBody, HttpEntity actualEntity) throws IOException { BytesReference expectedBytes = XContentHelper.toXContent(expectedBody, REQUEST_BODY_CONTENT_TYPE, false); assertEquals(XContentType.JSON.mediaTypeWithoutParameters(), actualEntity.getContentType().getValue());