REST high-level client: add support for exists alias (#28332)

Relates to #27205
This commit is contained in:
Luca Cavanna 2018-01-25 14:53:27 +01:00 committed by GitHub
parent 45e1fe8bfc
commit fd66c94ce1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 278 additions and 43 deletions

View File

@ -21,6 +21,7 @@ package org.elasticsearch.client;
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;
@ -35,7 +36,8 @@ import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
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.
@ -57,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);
}
/**
@ -68,7 +70,7 @@ public final class IndicesClient {
*/
public void deleteAsync(DeleteIndexRequest deleteIndexRequest, ActionListener<DeleteIndexResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(deleteIndexRequest, Request::deleteIndex, DeleteIndexResponse::fromXContent,
listener, Collections.emptySet(), headers);
listener, emptySet(), headers);
}
/**
@ -79,7 +81,7 @@ public final class IndicesClient {
*/
public CreateIndexResponse create(CreateIndexRequest createIndexRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(createIndexRequest, Request::createIndex, CreateIndexResponse::fromXContent,
Collections.emptySet(), headers);
emptySet(), headers);
}
/**
@ -90,7 +92,7 @@ public final class IndicesClient {
*/
public void createAsync(CreateIndexRequest createIndexRequest, ActionListener<CreateIndexResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(createIndexRequest, Request::createIndex, CreateIndexResponse::fromXContent,
listener, Collections.emptySet(), headers);
listener, emptySet(), headers);
}
/**
@ -101,7 +103,7 @@ public final class IndicesClient {
*/
public PutMappingResponse putMapping(PutMappingRequest putMappingRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(putMappingRequest, Request::putMapping, PutMappingResponse::fromXContent,
Collections.emptySet(), headers);
emptySet(), headers);
}
/**
@ -113,7 +115,7 @@ public final class IndicesClient {
public void putMappingAsync(PutMappingRequest putMappingRequest, ActionListener<PutMappingResponse> listener,
Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(putMappingRequest, Request::putMapping, PutMappingResponse::fromXContent,
listener, Collections.emptySet(), headers);
listener, emptySet(), headers);
}
/**
@ -125,7 +127,7 @@ public final class IndicesClient {
*/
public IndicesAliasesResponse updateAliases(IndicesAliasesRequest indicesAliasesRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(indicesAliasesRequest, Request::updateAliases,
IndicesAliasesResponse::fromXContent, Collections.emptySet(), headers);
IndicesAliasesResponse::fromXContent, emptySet(), headers);
}
/**
@ -138,7 +140,7 @@ public final class IndicesClient {
public void updateAliasesAsync(IndicesAliasesRequest indicesAliasesRequestRequest, ActionListener<IndicesAliasesResponse> listener,
Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(indicesAliasesRequestRequest, Request::updateAliases,
IndicesAliasesResponse::fromXContent, listener, Collections.emptySet(), headers);
IndicesAliasesResponse::fromXContent, listener, emptySet(), headers);
}
/**
@ -149,7 +151,7 @@ public final class IndicesClient {
*/
public OpenIndexResponse open(OpenIndexRequest openIndexRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent,
Collections.emptySet(), headers);
emptySet(), headers);
}
/**
@ -160,7 +162,7 @@ public final class IndicesClient {
*/
public void openAsync(OpenIndexRequest openIndexRequest, ActionListener<OpenIndexResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(openIndexRequest, Request::openIndex, OpenIndexResponse::fromXContent,
listener, Collections.emptySet(), headers);
listener, emptySet(), headers);
}
/**
@ -171,7 +173,7 @@ public final class IndicesClient {
*/
public CloseIndexResponse close(CloseIndexRequest closeIndexRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(closeIndexRequest, Request::closeIndex, CloseIndexResponse::fromXContent,
Collections.emptySet(), headers);
emptySet(), headers);
}
/**
@ -182,6 +184,28 @@ public final class IndicesClient {
*/
public void closeAsync(CloseIndexRequest closeIndexRequest, ActionListener<CloseIndexResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(closeIndexRequest, Request::closeIndex, CloseIndexResponse::fromXContent,
listener, Collections.emptySet(), headers);
listener, emptySet(), headers);
}
/**
* Checks if one or more aliases exist using the Aliases Exist API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html">
* Indices Aliases API on elastic.co</a>
*/
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
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html">
* Indices Aliases API on elastic.co</a>
*/
public void existsAliasAsync(GetAliasesRequest getAliasesRequest, ActionListener<Boolean> listener, Header... headers) {
restHighLevelClient.performRequestAsync(getAliasesRequest, Request::existsAlias, RestHighLevelClient::convertExistsResponse,
listener, emptySet(), headers);
}
}

View File

@ -29,6 +29,7 @@ import org.apache.http.entity.ByteArrayEntity;
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;
@ -133,7 +134,7 @@ public final class Request {
}
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());
@ -144,7 +145,7 @@ public final class Request {
}
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 @@ public final class Request {
}
static Request closeIndex(CloseIndexRequest closeIndexRequest) {
String endpoint = endpoint(closeIndexRequest.indices(), Strings.EMPTY_ARRAY, "_close");
String endpoint = endpoint(closeIndexRequest.indices(), "_close");
Params parameters = Params.builder();
@ -169,7 +170,7 @@ public final class Request {
}
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());
@ -472,13 +473,44 @@ public final class Request {
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());
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);
}
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 +518,9 @@ public final class Request {
}
/**
* 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 +688,11 @@ public final class Request {
return this;
}
Params withLocal(boolean local) {
putParam("local", Boolean.toString(local));
return this;
}
Map<String, String> getParams() {
return Collections.unmodifiableMap(params);
}

View File

@ -22,6 +22,7 @@ package org.elasticsearch.client;
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 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
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));
createIndex("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<String, Object> getIndexMetadata(String index) throws IOException {
Response response = client().performRequest("GET", index);

View File

@ -30,6 +30,7 @@ import org.apache.http.entity.ContentType;
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,44 @@ public class RequestTests extends ESTestCase {
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);
//the HEAD endpoint requires at least an alias or an index
String[] aliases = randomIndicesNames(indices.length == 0 ? 1 : 0, 5);
getAliasesRequest.aliases(aliases);
Map<String, String> 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 expectedEndpoint = new StringJoiner("/", "/", "");
String index = String.join(",", indices);
if (Strings.hasLength(index)) {
expectedEndpoint.add(index);
}
expectedEndpoint.add("_alias");
String alias = String.join(",", aliases);
if (Strings.hasLength(alias)) {
expectedEndpoint.add(alias);
}
assertEquals(expectedEndpoint.toString(), request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
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());
@ -1017,14 +1056,25 @@ public class RequestTests extends ESTestCase {
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() {

View File

@ -22,6 +22,7 @@ package org.elasticsearch.client.documentation;
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;
@ -304,16 +305,10 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
}
});
// 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());
});
}
}
public void testOpenIndex() throws IOException {
public void testOpenIndex() throws Exception {
RestHighLevelClient client = highLevelClient();
{
@ -384,7 +379,7 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
}
}
public void testCloseIndex() throws IOException {
public void testCloseIndex() throws Exception {
RestHighLevelClient client = highLevelClient();
{
@ -432,19 +427,59 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
}
});
// end::close-index-execute-async
}
}
public void testExistsAlias() throws Exception {
RestHighLevelClient client = highLevelClient();
{
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index")
.alias(new Alias("alias")));
assertTrue(createIndexResponse.isAcknowledged());
}
{
// 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) {
// 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<Boolean>() {
@Override
public void onResponse(Boolean exists) {
// <1>
}
}
// end::close-index-notfound
@Override
public void onFailure(Exception e) {
// <2>
}
});
// end::exists-alias-execute-async
}
}

View File

@ -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) was found or not.

View File

@ -10,6 +10,8 @@ include::putmapping.asciidoc[]
include::update_aliases.asciidoc[]
include::exists_alias.asciidoc[]
include::_index.asciidoc[]
include::get.asciidoc[]

View File

@ -10,6 +10,7 @@ Indices APIs::
* <<java-rest-high-close-index>>
* <<java-rest-high-put-mapping>>
* <<java-rest-high-update-aliases>>
* <<java-rest-high-exists-alias>>
Single document APIs::
* <<java-rest-high-document-index>>