Skip to content

Commit

Permalink
Add Refresh API for RestHighLevelClient (elastic#27799)
Browse files Browse the repository at this point in the history
Relates to elastic#27205
  • Loading branch information
PnPie authored and javanna committed Feb 28, 2018
1 parent 26405a9 commit f48fca5
Show file tree
Hide file tree
Showing 14 changed files with 600 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;

Expand Down Expand Up @@ -215,6 +217,26 @@ public void existsAliasAsync(GetAliasesRequest getAliasesRequest, ActionListener
listener, emptySet(), headers);
}

/**
* Refresh one or more indices using the Refresh API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html"> Refresh API on elastic.co</a>
*/
public RefreshResponse refresh(RefreshRequest refreshRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(refreshRequest, Request::refresh, RefreshResponse::fromXContent,
emptySet(), headers);
}

/**
* Asynchronously refresh one or more indices using the Refresh API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html"> Refresh API on elastic.co</a>
*/
public void refreshAsync(RefreshRequest refreshRequest, ActionListener<RefreshResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(refreshRequest, Request::refresh, RefreshResponse::fromXContent,
listener, emptySet(), headers);
}

/**
* Checks if the index (indices) exists or not.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
Expand Down Expand Up @@ -216,6 +217,15 @@ static Request putMapping(PutMappingRequest putMappingRequest) throws IOExceptio
return new Request(HttpPut.METHOD_NAME, endpoint, parameters.getParams(), entity);
}

static Request refresh(RefreshRequest refreshRequest) {
String endpoint = endpoint(refreshRequest.indices(), "_refresh");

Params parameters = Params.builder();
parameters.withIndicesOptions(refreshRequest.indicesOptions());

return new Request(HttpPost.METHOD_NAME, endpoint, parameters.getParams(), null);
}

static Request info() {
return new Request(HttpGet.METHOD_NAME, "/", Collections.emptyMap(), null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
Expand All @@ -47,6 +49,7 @@
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.support.broadcast.BroadcastResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
Expand Down Expand Up @@ -381,6 +384,32 @@ public void testCloseNonExistentIndex() throws IOException {
assertEquals(RestStatus.NOT_FOUND, exception.status());
}

public void testRefresh() throws IOException {
{
String index = "index";
Settings settings = Settings.builder()
.put("number_of_shards", 1)
.put("number_of_replicas", 0)
.build();
createIndex(index, settings);
RefreshRequest refreshRequest = new RefreshRequest(index);
RefreshResponse refreshResponse =
execute(refreshRequest, highLevelClient().indices()::refresh, highLevelClient().indices()::refreshAsync);
assertThat(refreshResponse.getTotalShards(), equalTo(1));
assertThat(refreshResponse.getSuccessfulShards(), equalTo(1));
assertThat(refreshResponse.getFailedShards(), equalTo(0));
assertThat(refreshResponse.getShardFailures(), equalTo(BroadcastResponse.EMPTY));
}
{
String nonExistentIndex = "non_existent_index";
assertFalse(indexExists(nonExistentIndex));
RefreshRequest refreshRequest = new RefreshRequest(nonExistentIndex);
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
() -> execute(refreshRequest, highLevelClient().indices()::refresh, highLevelClient().indices()::refreshAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
}
}

public void testExistsAlias() throws IOException {
GetAliasesRequest getAliasesRequest = new GetAliasesRequest("alias");
assertFalse(execute(getAliasesRequest, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync));
Expand Down Expand Up @@ -495,4 +524,4 @@ public void testRollover() throws IOException {
assertEquals("test_new", rolloverResponse.getNewIndex());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
Expand Down Expand Up @@ -535,6 +536,21 @@ public void testIndex() throws IOException {
}
}

public void testRefresh() {
String[] indices = randomIndicesNames(1, 5);
RefreshRequest refreshRequest = new RefreshRequest(indices);

Map<String, String> expectedParams = new HashMap<>();
setRandomIndicesOptions(refreshRequest::indicesOptions, refreshRequest::indicesOptions, expectedParams);

Request request = Request.refresh(refreshRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "").add(String.join(",", indices)).add("_refresh");
assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
assertThat(request.getParameters(), equalTo(expectedParams));
assertThat(request.getEntity(), nullValue());
assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME));
}

public void testUpdate() throws IOException {
XContentType xContentType = randomFrom(XContentType.values());

Expand Down Expand Up @@ -1058,7 +1074,7 @@ public void testExistsAliasNoAliasNoIndex() {
IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> Request.existsAlias(getAliasesRequest));
assertEquals("existsAlias requires at least an alias or an index", iae.getMessage());
}

public void testRankEval() throws Exception {
RankEvalSpec spec = new RankEvalSpec(
Collections.singletonList(new RatedRequest("queryId", Collections.emptyList(), new SearchSourceBuilder())),
Expand Down Expand Up @@ -1130,7 +1146,7 @@ private static void resizeTest(ResizeType resizeType, CheckedFunction<ResizeRequ
assertEquals(expectedParams, request.getParameters());
assertToXContentBody(resizeRequest, request.getEntity());
}

public void testClusterPutSettings() throws IOException {
ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest();
Map<String, String> expectedParams = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
import org.elasticsearch.client.RestHighLevelClient;
Expand Down Expand Up @@ -620,6 +623,74 @@ public void onFailure(Exception e) {
}
}

public void testRefreshIndex() throws Exception {
RestHighLevelClient client = highLevelClient();

{
createIndex("index1", Settings.EMPTY);
}

{
// tag::refresh-request
RefreshRequest request = new RefreshRequest("index1"); // <1>
RefreshRequest requestMultiple = new RefreshRequest("index1", "index2"); // <2>
RefreshRequest requestAll = new RefreshRequest(); // <3>
// end::refresh-request

// tag::refresh-request-indicesOptions
request.indicesOptions(IndicesOptions.lenientExpandOpen()); // <1>
// end::refresh-request-indicesOptions

// tag::refresh-execute
RefreshResponse refreshResponse = client.indices().refresh(request);
// end::refresh-execute

// tag::refresh-response
int totalShards = refreshResponse.getTotalShards(); // <1>
int successfulShards = refreshResponse.getSuccessfulShards(); // <2>
int failedShards = refreshResponse.getFailedShards(); // <3>
DefaultShardOperationFailedException[] failures = refreshResponse.getShardFailures(); // <4>
// end::refresh-response

// tag::refresh-execute-listener
ActionListener<RefreshResponse> listener = new ActionListener<RefreshResponse>() {
@Override
public void onResponse(RefreshResponse refreshResponse) {
// <1>
}

@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::refresh-execute-listener

// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);

// tag::refresh-execute-async
client.indices().refreshAsync(request, listener); // <1>
// end::refresh-execute-async

assertTrue(latch.await(30L, TimeUnit.SECONDS));
}

{
// tag::refresh-notfound
try {
RefreshRequest request = new RefreshRequest("does_not_exist");
client.indices().refresh(request);
} catch (ElasticsearchException exception) {
if (exception.status() == RestStatus.NOT_FOUND) {
// <1>
}
}
// end::refresh-notfound
}
}

public void testCloseIndex() throws Exception {
RestHighLevelClient client = highLevelClient();

Expand Down
84 changes: 84 additions & 0 deletions docs/java-rest/high-level/indices/refresh.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[[java-rest-high-refresh]]
=== Refresh API

[[java-rest-high-refresh-request]]
==== Refresh Request

A `RefreshRequest` can be applied to one or more indices, or even on `_all` the indices:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[refresh-request]
--------------------------------------------------
<1> Refresh one index
<2> Refresh multiple indices
<3> Refresh all the indices

==== Optional arguments

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[refresh-request-indicesOptions]
--------------------------------------------------
<1> Setting `IndicesOptions` controls how unavailable indices are resolved and
how wildcard expressions are expanded

[[java-rest-high-refresh-sync]]
==== Synchronous Execution

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[refresh-execute]
--------------------------------------------------

[[java-rest-high-refresh-async]]
==== Asynchronous Execution

The asynchronous execution of a refresh request requires both the `RefreshRequest`
instance and an `ActionListener` instance to be passed to the asynchronous
method:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[refresh-execute-async]
--------------------------------------------------
<1> The `RefreshRequest` to execute and the `ActionListener` to use when
the execution completes

The asynchronous method does not block and returns immediately. Once it is
completed the `ActionListener` is called back using the `onResponse` method
if the execution successfully completed or using the `onFailure` method if
it failed.

A typical listener for `RefreshResponse` looks like:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[refresh-execute-listener]
--------------------------------------------------
<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-refresh-response]]
==== Refresh Response

The returned `RefreshResponse` allows to retrieve information about the
executed operation as follows:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[refresh-response]
--------------------------------------------------
<1> Total number of shards hit by the refresh request
<2> Number of shards where the refresh has succeeded
<3> Number of shards where the refresh has failed
<4> A list of failures if the operation failed on one or more shards

By default, if the indices were not found, an `ElasticsearchException` will be thrown:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[refresh-notfound]
--------------------------------------------------
<1> Do something if the indices to be refreshed were not found
2 changes: 2 additions & 0 deletions docs/java-rest/high-level/supported-apis.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Index Management::
* <<java-rest-high-close-index>>
* <<java-rest-high-shrink-index>>
* <<java-rest-high-split-index>>
* <<java-rest-high-refresh>>
* <<java-rest-high-rollover-index>>

Mapping Management::
Expand All @@ -68,6 +69,7 @@ include::indices/open_index.asciidoc[]
include::indices/close_index.asciidoc[]
include::indices/shrink_index.asciidoc[]
include::indices/split_index.asciidoc[]
include::indices/refresh.asciidoc[]
include::indices/rollover.asciidoc[]
include::indices/put_mapping.asciidoc[]
include::indices/update_aliases.asciidoc[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.action.admin.indices.refresh;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.support.broadcast.BroadcastRequest;

/**
Expand Down
Loading

0 comments on commit f48fca5

Please sign in to comment.