|
@@ -24,6 +24,7 @@ import org.elasticsearch.action.ActionListener;
|
|
|
import org.elasticsearch.action.LatchedActionListener;
|
|
|
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
|
|
|
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse;
|
|
|
+import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
|
|
|
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
|
|
|
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
|
|
|
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
|
@@ -35,6 +36,7 @@ import org.elasticsearch.client.RestHighLevelClient;
|
|
|
import org.elasticsearch.client.ccr.PauseFollowRequest;
|
|
|
import org.elasticsearch.client.ccr.PutFollowRequest;
|
|
|
import org.elasticsearch.client.ccr.PutFollowResponse;
|
|
|
+import org.elasticsearch.client.ccr.UnfollowRequest;
|
|
|
import org.elasticsearch.client.core.AcknowledgedResponse;
|
|
|
import org.elasticsearch.common.xcontent.XContentHelper;
|
|
|
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
|
@@ -217,6 +219,91 @@ public class CCRDocumentationIT extends ESRestHighLevelClientTestCase {
|
|
|
assertTrue(latch.await(30L, TimeUnit.SECONDS));
|
|
|
}
|
|
|
|
|
|
+ public void testUnfollow() throws Exception {
|
|
|
+ RestHighLevelClient client = highLevelClient();
|
|
|
+ {
|
|
|
+ // Create leader index:
|
|
|
+ CreateIndexRequest createIndexRequest = new CreateIndexRequest("leader");
|
|
|
+ createIndexRequest.settings(Collections.singletonMap("index.soft_deletes.enabled", true));
|
|
|
+ CreateIndexResponse response = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
|
|
|
+ assertThat(response.isAcknowledged(), is(true));
|
|
|
+ }
|
|
|
+ String followIndex = "follower";
|
|
|
+ // Follow index, pause and close, so that it can be unfollowed:
|
|
|
+ {
|
|
|
+ PutFollowRequest putFollowRequest = new PutFollowRequest("local", "leader", followIndex);
|
|
|
+ PutFollowResponse putFollowResponse = client.ccr().putFollow(putFollowRequest, RequestOptions.DEFAULT);
|
|
|
+ assertThat(putFollowResponse.isFollowIndexCreated(), is(true));
|
|
|
+ assertThat(putFollowResponse.isFollowIndexShardsAcked(), is(true));
|
|
|
+ assertThat(putFollowResponse.isIndexFollowingStarted(), is(true));
|
|
|
+
|
|
|
+ PauseFollowRequest pauseFollowRequest = new PauseFollowRequest(followIndex);
|
|
|
+ AcknowledgedResponse unfollowResponse = client.ccr().pauseFollow(pauseFollowRequest, RequestOptions.DEFAULT);
|
|
|
+ assertThat(unfollowResponse.isAcknowledged(), is(true));
|
|
|
+
|
|
|
+ CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followIndex);
|
|
|
+ assertThat(client.indices().close(closeIndexRequest, RequestOptions.DEFAULT).isAcknowledged(), is(true));
|
|
|
+ }
|
|
|
+
|
|
|
+ // tag::ccr-unfollow-request
|
|
|
+ UnfollowRequest request = new UnfollowRequest(followIndex); // <1>
|
|
|
+ // end::ccr-unfollow-request
|
|
|
+
|
|
|
+ // tag::ccr-unfollow-execute
|
|
|
+ AcknowledgedResponse response =
|
|
|
+ client.ccr().unfollow(request, RequestOptions.DEFAULT);
|
|
|
+ // end::ccr-unfollow-execute
|
|
|
+
|
|
|
+ // tag::ccr-unfollow-response
|
|
|
+ boolean acknowledged = response.isAcknowledged(); // <1>
|
|
|
+ // end::ccr-unfollow-response
|
|
|
+
|
|
|
+ // Delete, put follow index, pause and close, so that it can be unfollowed again:
|
|
|
+ {
|
|
|
+ DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(followIndex);
|
|
|
+ assertThat(client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT).isAcknowledged(), is(true));
|
|
|
+
|
|
|
+ PutFollowRequest putFollowRequest = new PutFollowRequest("local", "leader", followIndex);
|
|
|
+ PutFollowResponse putFollowResponse = client.ccr().putFollow(putFollowRequest, RequestOptions.DEFAULT);
|
|
|
+ assertThat(putFollowResponse.isFollowIndexCreated(), is(true));
|
|
|
+ assertThat(putFollowResponse.isFollowIndexShardsAcked(), is(true));
|
|
|
+ assertThat(putFollowResponse.isIndexFollowingStarted(), is(true));
|
|
|
+
|
|
|
+ PauseFollowRequest pauseFollowRequest = new PauseFollowRequest(followIndex);
|
|
|
+ AcknowledgedResponse unfollowResponse = client.ccr().pauseFollow(pauseFollowRequest, RequestOptions.DEFAULT);
|
|
|
+ assertThat(unfollowResponse.isAcknowledged(), is(true));
|
|
|
+
|
|
|
+ CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followIndex);
|
|
|
+ assertThat(client.indices().close(closeIndexRequest, RequestOptions.DEFAULT).isAcknowledged(), is(true));
|
|
|
+ }
|
|
|
+
|
|
|
+ // tag::ccr-unfollow-execute-listener
|
|
|
+ ActionListener<AcknowledgedResponse> listener =
|
|
|
+ new ActionListener<AcknowledgedResponse>() {
|
|
|
+ @Override
|
|
|
+ public void onResponse(AcknowledgedResponse response) {
|
|
|
+ boolean acknowledged = response.isAcknowledged(); // <1>
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(Exception e) {
|
|
|
+ // <2>
|
|
|
+ }
|
|
|
+ };
|
|
|
+ // end::ccr-unfollow-execute-listener
|
|
|
+
|
|
|
+ // Replace the empty listener by a blocking listener in test
|
|
|
+ final CountDownLatch latch = new CountDownLatch(1);
|
|
|
+ listener = new LatchedActionListener<>(listener, latch);
|
|
|
+
|
|
|
+ // tag::ccr-unfollow-execute-async
|
|
|
+ client.ccr()
|
|
|
+ .unfollowAsync(request, RequestOptions.DEFAULT, listener); // <1>
|
|
|
+ // end::ccr-unfollow-execute-async
|
|
|
+
|
|
|
+ assertTrue(latch.await(30L, TimeUnit.SECONDS));
|
|
|
+ }
|
|
|
+
|
|
|
static Map<String, Object> toMap(Response response) throws IOException {
|
|
|
return XContentHelper.convertToMap(JsonXContent.jsonXContent, EntityUtils.toString(response.getEntity()), false);
|
|
|
}
|