Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for indices exists to REST high level client #27384

Merged
merged 31 commits into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a3a2f22
#27205: Indices exist.
Nov 14, 2017
5b6bd87
#27205: Indices exist. First tests.
Nov 14, 2017
d05262f
HLRC: More tests. First functional version.
Nov 14, 2017
2e4d42a
Reverted change (obsolete type declaration)
Nov 14, 2017
e57d2ff
Removed useless code.
Nov 14, 2017
abf2919
Updated docs.
Nov 14, 2017
a63880f
Code improvements.
Dec 1, 2017
b1379b6
Reused existing method. Params parsing.
Jan 26, 2018
2ea0363
Updated tests. Added more params.
Jan 26, 2018
902aa9b
Changed test name to be according to convention.
Jan 26, 2018
c5af2e7
Small refactoring in Request.Params
Jan 26, 2018
a14e05b
Handling the 'local' param.
Jan 26, 2018
da2ad92
Merge branch 'master' into hlrc-indices-exist
Jan 26, 2018
18e2c92
Reused existing method.
Jan 26, 2018
51ed9aa
Merge branch 'master' into hlrc-indices-exist
Jan 26, 2018
3491962
Testing linking in ascii docs.
Jan 26, 2018
53c681f
Merge branch 'master' into hlrc-indices-exist
Jan 29, 2018
f35fe50
Merge branch 'master' into hlrc-indices-exist
Jan 29, 2018
3aad6c1
Add flat_settings and include_defaults to GetIndexRequest.
Jan 29, 2018
0e6adfa
Replace IndicesExistRequest in HLRC with GetIndexRequest.
Jan 29, 2018
53d226c
Added 'human' parameter. Updated docs.
Jan 30, 2018
659a252
Randomized tests.
Jan 30, 2018
d665b55
Refactored tests.
Jan 31, 2018
3fd5006
Improved tests.
Jan 31, 2018
fe18f27
more JavaDocs.
Jan 31, 2018
1157286
Merge branch 'master' into hlrc-indices-exist
Jan 31, 2018
9461afb
Updated docs. Fixed a test.
Jan 31, 2018
910cc95
Updated docs. Added another check to test.
Feb 1, 2018
8df7602
Merge branch 'master' into hlrc-indices-exist
Feb 1, 2018
870fdfd
Fixed merge error.
Feb 2, 2018
1f57a30
Combined tests.
Feb 2, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you revert the addition of the static import? It affects also other methods that you are not changing and that causes some noise in the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do 👍


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,52 @@

public class IndicesClientIT extends ESRestHighLevelClientTestCase {

public void testIndexExistsIfIndexPresent() throws IOException {
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);
}

public void testIndexExistsIfIndexNotPresent() throws IOException {
String indexName = "non_existent_index";

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

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

public void testIndexExistsIfOneIndexPresentOneIsnt() throws IOException {
String existingIndex = "test_index_exists_index_present";
createIndex(existingIndex, Settings.EMPTY);

String nonExistentIndex = "non_existent_index";

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()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you assert that request.getEntity is null please?

assertNull(request.getEntity());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that one test here is enough. test it with a random number of indices ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Test is now using up to 10 indices with random names.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with setRandomIndicesOptions this should not be needed


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));
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good work!


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 @@ -70,6 +72,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
6 changes: 6 additions & 0 deletions docs/java-rest/high-level/apis/index.asciidoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
include::create_index.asciidoc[]

<<<<<<< HEAD
include::indices_exists.asciidoc[]

include::deleteindex.asciidoc[]
=======
include::delete_index.asciidoc[]
>>>>>>> master
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you fix this? A merge gone wrong I think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include::deleteindex.asciidoc[] should go, it's been replaced by include::delete_index.asciidoc[]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks for noticing it!


include::open_index.asciidoc[]

Expand Down
Loading