|
@@ -0,0 +1,133 @@
|
|
|
+/*
|
|
|
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
+ * or more contributor license agreements. Licensed under the Elastic License
|
|
|
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
|
|
|
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
|
|
|
+ * Side Public License, v 1.
|
|
|
+ */
|
|
|
+
|
|
|
+package org.elasticsearch.client.documentation;
|
|
|
+
|
|
|
+import org.elasticsearch.action.ActionListener;
|
|
|
+import org.elasticsearch.action.LatchedActionListener;
|
|
|
+import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest;
|
|
|
+import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest;
|
|
|
+import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
|
|
|
+import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
|
|
|
+import org.elasticsearch.action.index.IndexRequest;
|
|
|
+import org.elasticsearch.action.index.IndexResponse;
|
|
|
+import org.elasticsearch.action.support.WriteRequest;
|
|
|
+import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
|
|
+import org.elasticsearch.client.ESRestHighLevelClientTestCase;
|
|
|
+import org.elasticsearch.client.RequestOptions;
|
|
|
+import org.elasticsearch.client.RestHighLevelClient;
|
|
|
+import org.elasticsearch.client.indices.CreateIndexRequest;
|
|
|
+import org.elasticsearch.client.indices.CreateIndexResponse;
|
|
|
+import org.elasticsearch.client.searchable_snapshots.MountSnapshotRequest;
|
|
|
+import org.elasticsearch.common.settings.Settings;
|
|
|
+import org.elasticsearch.common.unit.TimeValue;
|
|
|
+import org.elasticsearch.common.xcontent.XContentType;
|
|
|
+import org.elasticsearch.repositories.fs.FsRepository;
|
|
|
+import org.elasticsearch.rest.RestStatus;
|
|
|
+import org.elasticsearch.snapshots.RestoreInfo;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.concurrent.CountDownLatch;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import static org.hamcrest.Matchers.is;
|
|
|
+
|
|
|
+public class SearchableSnapshotsDocumentationIT extends ESRestHighLevelClientTestCase {
|
|
|
+
|
|
|
+ public void testMountSnapshot() throws IOException, InterruptedException {
|
|
|
+ final RestHighLevelClient client = highLevelClient();
|
|
|
+ {
|
|
|
+ final CreateIndexRequest request = new CreateIndexRequest("index");
|
|
|
+ final CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
|
|
|
+ assertTrue(response.isAcknowledged());
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ final IndexRequest request = new IndexRequest("index")
|
|
|
+ .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
|
|
|
+ .source("{}", XContentType.JSON);
|
|
|
+ final IndexResponse response = client.index(request, RequestOptions.DEFAULT);
|
|
|
+ assertThat(response.status(), is(RestStatus.CREATED));
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ final PutRepositoryRequest request = new PutRepositoryRequest("repository");
|
|
|
+ request.settings("{\"location\": \".\"}", XContentType.JSON);
|
|
|
+ request.type(FsRepository.TYPE);
|
|
|
+ final AcknowledgedResponse response = client.snapshot().createRepository(request, RequestOptions.DEFAULT);
|
|
|
+ assertTrue(response.isAcknowledged());
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ final CreateSnapshotRequest request =
|
|
|
+ new CreateSnapshotRequest("repository", "snapshot").waitForCompletion(true);
|
|
|
+ final CreateSnapshotResponse response = client.snapshot().create(request, RequestOptions.DEFAULT);
|
|
|
+ assertThat(response.getSnapshotInfo().status(), is(RestStatus.OK));
|
|
|
+ }
|
|
|
+
|
|
|
+ // tag::searchable-snapshots-mount-snapshot-request
|
|
|
+ final MountSnapshotRequest request = new MountSnapshotRequest(
|
|
|
+ "repository", // <1>
|
|
|
+ "snapshot", // <2>
|
|
|
+ "index" // <3>
|
|
|
+ );
|
|
|
+ request.masterTimeout(TimeValue.timeValueSeconds(30)); // <4>
|
|
|
+ request.waitForCompletion(true); // <5>
|
|
|
+ request.storage(MountSnapshotRequest.Storage.FULL_COPY); // <6>
|
|
|
+ request.renamedIndex("renamed_index"); // <7>
|
|
|
+ final Settings indexSettings = Settings.builder()
|
|
|
+ .put("index.number_of_replicas", 0)
|
|
|
+ .build();
|
|
|
+ request.indexSettings(indexSettings); // <8>
|
|
|
+ request.ignoredIndexSettings(
|
|
|
+ new String[]{"index.refresh_interval"}); // <9>
|
|
|
+ // end::searchable-snapshots-mount-snapshot-request
|
|
|
+
|
|
|
+ // tag::searchable-snapshots-mount-snapshot-execute
|
|
|
+ final RestoreSnapshotResponse response = client
|
|
|
+ .searchableSnapshots()
|
|
|
+ .mountSnapshot(request, RequestOptions.DEFAULT);
|
|
|
+ // end::searchable-snapshots-mount-snapshot-execute
|
|
|
+
|
|
|
+ // tag::searchable-snapshots-mount-snapshot-response
|
|
|
+ final RestoreInfo restoreInfo = response.getRestoreInfo(); // <1>
|
|
|
+ // end::searchable-snapshots-mount-snapshot-response
|
|
|
+
|
|
|
+ // tag::searchable-snapshots-mount-snapshot-execute-listener
|
|
|
+ ActionListener<RestoreSnapshotResponse> listener =
|
|
|
+ new ActionListener<RestoreSnapshotResponse>() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onResponse(
|
|
|
+ final RestoreSnapshotResponse response) { // <1>
|
|
|
+ final RestoreInfo restoreInfo = response.getRestoreInfo();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(final Exception e) {
|
|
|
+ // <2>
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+ // end::searchable-snapshots-mount-snapshot-execute-listener
|
|
|
+
|
|
|
+ final CountDownLatch latch = new CountDownLatch(1);
|
|
|
+ listener = new LatchedActionListener<>(listener, latch);
|
|
|
+
|
|
|
+ // tag::searchable-snapshots-mount-snapshot-execute-async
|
|
|
+ client.searchableSnapshots().mountSnapshotAsync(
|
|
|
+ request,
|
|
|
+ RequestOptions.DEFAULT,
|
|
|
+ listener // <1>
|
|
|
+ );
|
|
|
+ // end::searchable-snapshots-mount-snapshot-execute-async
|
|
|
+
|
|
|
+ assertTrue(latch.await(30L, TimeUnit.SECONDS));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|