Skip to content

Commit

Permalink
Add support for indices exists to REST high level client (elastic#27384)
Browse files Browse the repository at this point in the history
Relates to elastic#27205
  • Loading branch information
hariso authored and javanna committed Feb 2, 2018
1 parent 031415a commit 897ef45
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
Expand All @@ -38,6 +39,7 @@
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;

import java.io.IOException;
import java.util.Collections;

import static java.util.Collections.emptySet;

Expand Down Expand Up @@ -211,6 +213,39 @@ public void existsAliasAsync(GetAliasesRequest getAliasesRequest, ActionListener
listener, emptySet(), headers);
}

/**
* Checks if the index (indices) exists or not.
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html">
* Indices Exists API on elastic.co</a>
*/
public boolean exists(GetIndexRequest request, Header... headers) throws IOException {
return restHighLevelClient.performRequest(
request,
Request::indicesExist,
RestHighLevelClient::convertExistsResponse,
Collections.emptySet(),
headers
);
}

/**
* Asynchronously checks if the index (indices) exists or not.
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html">
* Indices Exists API on elastic.co</a>
*/
public void existsAsync(GetIndexRequest request, ActionListener<Boolean> listener, Header... headers) {
restHighLevelClient.performRequestAsync(
request,
Request::indicesExist,
RestHighLevelClient::convertExistsResponse,
listener,
Collections.emptySet(),
headers
);
}

/**
* Shrinks an index using the Shrink Index API
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
Expand Down Expand Up @@ -582,6 +583,17 @@ public static ContentType createContentType(final XContentType xContentType) {
return ContentType.create(xContentType.mediaTypeWithoutParameters(), (Charset) null);
}

static Request indicesExist(GetIndexRequest request) {
String endpoint = endpoint(request.indices(), Strings.EMPTY_ARRAY, "");
Params params = Params.builder();
params.withLocal(request.local());
params.withHuman(request.humanReadable());
params.withIndicesOptions(request.indicesOptions());
params.withFlatSettings(request.flatSettings());
params.withIncludeDefaults(request.includeDefaults());
return new Request(HttpHead.METHOD_NAME, endpoint, params.getParams(), null);
}

/**
* Utility class to build request's parameters map and centralize all parameter names.
*/
Expand Down Expand Up @@ -729,8 +741,31 @@ Params withIndicesOptions(IndicesOptions indicesOptions) {
return this;
}

Params withHuman(boolean human) {
if (human) {
putParam("human", Boolean.toString(human));
}
return this;
}

Params withLocal(boolean local) {
putParam("local", Boolean.toString(local));
if (local) {
putParam("local", Boolean.toString(local));
}
return this;
}

Params withFlatSettings(boolean flatSettings) {
if (flatSettings) {
return putParam("flat_settings", Boolean.TRUE.toString());
}
return this;
}

Params withIncludeDefaults(boolean includeDefaults) {
if (includeDefaults) {
return putParam("include_defaults", Boolean.TRUE.toString());
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
Expand All @@ -58,6 +59,58 @@

public class IndicesClientIT extends ESRestHighLevelClientTestCase {

public void testIndicesExists() throws IOException {
// Index present
{
String indexName = "test_index_exists_index_present";
createIndex(indexName, Settings.EMPTY);

GetIndexRequest request = new GetIndexRequest();
request.indices(indexName);

boolean response = execute(
request,
highLevelClient().indices()::exists,
highLevelClient().indices()::existsAsync
);
assertTrue(response);
}

// Index doesn't exist
{
String indexName = "non_existent_index";

GetIndexRequest request = new GetIndexRequest();
request.indices(indexName);

boolean response = execute(
request,
highLevelClient().indices()::exists,
highLevelClient().indices()::existsAsync
);
assertFalse(response);
}

// One index exists, one doesn't
{
String existingIndex = "apples";
createIndex(existingIndex, Settings.EMPTY);

String nonExistentIndex = "oranges";

GetIndexRequest request = new GetIndexRequest();
request.indices(existingIndex, nonExistentIndex);

boolean response = execute(
request,
highLevelClient().indices()::exists,
highLevelClient().indices()::existsAsync
);
assertFalse(response);
}

}

@SuppressWarnings({"unchecked", "rawtypes"})
public void testCreateIndex() throws IOException {
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
Expand All @@ -55,6 +56,7 @@
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.action.support.master.MasterNodeReadRequest;
import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.action.support.replication.ReplicationRequest;
import org.elasticsearch.action.update.UpdateRequest;
Expand Down Expand Up @@ -262,6 +264,26 @@ public void testExists() {
getAndExistsTest(Request::exists, HttpHead.METHOD_NAME);
}

public void testIndicesExist() {
String[] indices = randomIndicesNames(1, 10);

GetIndexRequest getIndexRequest = new GetIndexRequest().indices(indices);

Map<String, String> expectedParams = new HashMap<>();
setRandomIndicesOptions(getIndexRequest::indicesOptions, getIndexRequest::indicesOptions, expectedParams);
setRandomLocal(getIndexRequest, expectedParams);
setRandomFlatSettings(getIndexRequest, expectedParams);
setRandomHumanReadable(getIndexRequest, expectedParams);
setRandomIncludeDefaults(getIndexRequest, expectedParams);

final Request request = Request.indicesExist(getIndexRequest);

assertEquals(HttpHead.METHOD_NAME, request.getMethod());
assertEquals("/" + String.join(",", indices), request.getEndpoint());
assertThat(expectedParams, equalTo(request.getParameters()));
assertNull(request.getEntity());
}

private static void getAndExistsTest(Function<GetRequest, Request> requestConverter, String method) {
String index = randomAlphaOfLengthBetween(3, 10);
String type = randomAlphaOfLengthBetween(3, 10);
Expand Down Expand Up @@ -1008,12 +1030,7 @@ public void testExistsAlias() {
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()));

setRandomLocal(getAliasesRequest, expectedParams);
setRandomIndicesOptions(getAliasesRequest::indicesOptions, getAliasesRequest::indicesOptions, expectedParams);

Request request = Request.existsAlias(getAliasesRequest);
Expand Down Expand Up @@ -1252,6 +1269,46 @@ private static void setRandomIndicesOptions(Consumer<IndicesOptions> setter, Sup
}
}

private static void setRandomIncludeDefaults(GetIndexRequest request, Map<String, String> expectedParams) {
if (randomBoolean()) {
boolean includeDefaults = randomBoolean();
request.includeDefaults(includeDefaults);
if (includeDefaults) {
expectedParams.put("include_defaults", String.valueOf(includeDefaults));
}
}
}

private static void setRandomHumanReadable(GetIndexRequest request, Map<String, String> expectedParams) {
if (randomBoolean()) {
boolean humanReadable = randomBoolean();
request.humanReadable(humanReadable);
if (humanReadable) {
expectedParams.put("human", String.valueOf(humanReadable));
}
}
}

private static void setRandomFlatSettings(GetIndexRequest request, Map<String, String> expectedParams) {
if (randomBoolean()) {
boolean flatSettings = randomBoolean();
request.flatSettings(flatSettings);
if (flatSettings) {
expectedParams.put("flat_settings", String.valueOf(flatSettings));
}
}
}

private static void setRandomLocal(MasterNodeReadRequest<?> request, Map<String, String> expectedParams) {
if (randomBoolean()) {
boolean local = randomBoolean();
request.local(local);
if (local) {
expectedParams.put("local", String.valueOf(local));
}
}
}

private static void setRandomTimeout(Consumer<String> setter, TimeValue defaultTimeout, Map<String, String> expectedParams) {
if (randomBoolean()) {
String timeout = randomTimeValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.client.documentation;

import org.apache.http.Header;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.LatchedActionListener;
Expand All @@ -33,6 +34,7 @@
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
Expand Down Expand Up @@ -74,6 +76,69 @@
*/
public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase {

public void testIndicesExist() throws IOException {
RestHighLevelClient client = highLevelClient();

{
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"));
assertTrue(createIndexResponse.isAcknowledged());
}

{
// tag::indices-exists-request
GetIndexRequest request = new GetIndexRequest();
request.indices("twitter"); // <1>
// end::indices-exists-request

IndicesOptions indicesOptions = IndicesOptions.strictExpand();
// tag::indices-exists-request-optionals
request.local(false); // <1>
request.humanReadable(true); // <2>
request.includeDefaults(false); // <3>
request.flatSettings(false); // <4>
request.indicesOptions(indicesOptions); // <5>
// end::indices-exists-request-optionals

Header[] headers = new Header[0];
// tag::indices-exists-response
boolean exists = client.indices().exists(request, headers);
// end::indices-exists-response
assertTrue(exists);
}
}

public void testIndicesExistAsync() throws IOException {
RestHighLevelClient client = highLevelClient();

{
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"));
assertTrue(createIndexResponse.isAcknowledged());
}

{
GetIndexRequest request = new GetIndexRequest();
request.indices("twitter");
Header[] headers = new Header[0];

// tag::indices-exists-async
client.indices().existsAsync(
request,
new ActionListener<Boolean>() {
@Override
public void onResponse(Boolean exists) {
// <1>
}

@Override
public void onFailure(Exception e) {
// <2>
}
},
headers
);
// end::indices-exists-async
}
}
public void testDeleteIndex() throws IOException {
RestHighLevelClient client = highLevelClient();

Expand Down
2 changes: 2 additions & 0 deletions docs/java-rest/high-level/apis/index.asciidoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
include::create_index.asciidoc[]

include::indices_exists.asciidoc[]

include::delete_index.asciidoc[]

include::open_index.asciidoc[]
Expand Down
Loading

0 comments on commit 897ef45

Please sign in to comment.