Search in sources :

Example 71 with RestHighLevelClient

use of org.opensearch.client.RestHighLevelClient in project OpenSearch by opensearch-project.

the class SnapshotClientDocumentationIT method testSnapshotSnapshotsStatus.

public void testSnapshotSnapshotsStatus() throws IOException {
    RestHighLevelClient client = highLevelClient();
    createTestRepositories();
    createTestIndex();
    createTestSnapshots();
    // tag::snapshots-status-request
    SnapshotsStatusRequest request = new SnapshotsStatusRequest();
    // end::snapshots-status-request
    // tag::snapshots-status-request-repository
    // <1>
    request.repository(repositoryName);
    // end::snapshots-status-request-repository
    // tag::snapshots-status-request-snapshots
    String[] snapshots = new String[] { snapshotName };
    // <1>
    request.snapshots(snapshots);
    // end::snapshots-status-request-snapshots
    // tag::snapshots-status-request-ignoreUnavailable
    // <1>
    request.ignoreUnavailable(true);
    // end::snapshots-status-request-ignoreUnavailable
    // tag::snapshots-status-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::snapshots-status-request-masterTimeout
    // tag::snapshots-status-execute
    SnapshotsStatusResponse response = client.snapshot().status(request, RequestOptions.DEFAULT);
    // end::snapshots-status-execute
    // tag::snapshots-status-response
    List<SnapshotStatus> snapshotStatusesResponse = response.getSnapshots();
    // <1>
    SnapshotStatus snapshotStatus = snapshotStatusesResponse.get(0);
    // <2>
    SnapshotsInProgress.State snapshotState = snapshotStatus.getState();
    // <3>
    SnapshotStats shardStats = snapshotStatus.getIndices().get(indexName).getShards().get(0).getStats();
    // end::snapshots-status-response
    assertThat(snapshotStatusesResponse.size(), equalTo(1));
    assertThat(snapshotStatusesResponse.get(0).getSnapshot().getRepository(), equalTo(SnapshotClientDocumentationIT.repositoryName));
    assertThat(snapshotStatusesResponse.get(0).getSnapshot().getSnapshotId().getName(), equalTo(snapshotName));
    assertThat(snapshotState.completed(), equalTo(true));
}
Also used : SnapshotsStatusResponse(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse) SnapshotStatus(org.opensearch.action.admin.cluster.snapshots.status.SnapshotStatus) SnapshotStats(org.opensearch.action.admin.cluster.snapshots.status.SnapshotStats) SnapshotsStatusRequest(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusRequest) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) RestHighLevelClient(org.opensearch.client.RestHighLevelClient)

Example 72 with RestHighLevelClient

use of org.opensearch.client.RestHighLevelClient in project OpenSearch by opensearch-project.

the class SnapshotClientDocumentationIT method testRestoreSnapshot.

public void testRestoreSnapshot() throws IOException {
    RestHighLevelClient client = highLevelClient();
    createTestRepositories();
    createTestIndex();
    createTestSnapshots();
    // tag::restore-snapshot-request
    RestoreSnapshotRequest request = new RestoreSnapshotRequest(repositoryName, snapshotName);
    // end::restore-snapshot-request
    // we need to restore as a different index name
    // tag::restore-snapshot-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::restore-snapshot-request-masterTimeout
    // tag::restore-snapshot-request-waitForCompletion
    // <1>
    request.waitForCompletion(true);
    // end::restore-snapshot-request-waitForCompletion
    // tag::restore-snapshot-request-partial
    // <1>
    request.partial(false);
    // end::restore-snapshot-request-partial
    // tag::restore-snapshot-request-include-global-state
    // <1>
    request.includeGlobalState(false);
    // end::restore-snapshot-request-include-global-state
    // tag::restore-snapshot-request-include-aliases
    // <1>
    request.includeAliases(false);
    // end::restore-snapshot-request-include-aliases
    // tag::restore-snapshot-request-indices
    // <1>
    request.indices("test_index");
    // end::restore-snapshot-request-indices
    String restoredIndexName = "restored_index";
    // tag::restore-snapshot-request-rename
    // <1>
    request.renamePattern("test_(.+)");
    // <2>
    request.renameReplacement("restored_$1");
    // end::restore-snapshot-request-rename
    // tag::restore-snapshot-request-index-settings
    // <1>
    request.indexSettings(Settings.builder().put("index.number_of_replicas", 0).build());
    // <2>
    request.ignoreIndexSettings("index.refresh_interval", "index.search.idle.after");
    request.indicesOptions(new // <3>
    IndicesOptions(EnumSet.of(IndicesOptions.Option.IGNORE_UNAVAILABLE), EnumSet.of(IndicesOptions.WildcardStates.OPEN)));
    // end::restore-snapshot-request-index-settings
    // tag::restore-snapshot-execute
    RestoreSnapshotResponse response = client.snapshot().restore(request, RequestOptions.DEFAULT);
    // end::restore-snapshot-execute
    // tag::restore-snapshot-response
    RestoreInfo restoreInfo = response.getRestoreInfo();
    // <1>
    List<String> indices = restoreInfo.indices();
    // end::restore-snapshot-response
    assertEquals(Collections.singletonList(restoredIndexName), indices);
    assertEquals(0, restoreInfo.failedShards());
    assertTrue(restoreInfo.successfulShards() > 0);
}
Also used : RestoreInfo(org.opensearch.snapshots.RestoreInfo) RestoreSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 73 with RestHighLevelClient

use of org.opensearch.client.RestHighLevelClient in project OpenSearch by opensearch-project.

the class SnapshotClientDocumentationIT method testSnapshotVerifyRepository.

public void testSnapshotVerifyRepository() throws IOException {
    RestHighLevelClient client = highLevelClient();
    createTestRepositories();
    // tag::verify-repository-request
    VerifyRepositoryRequest request = new VerifyRepositoryRequest(repositoryName);
    // end::verify-repository-request
    // tag::verify-repository-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::verify-repository-request-masterTimeout
    // tag::verify-repository-request-timeout
    // <1>
    request.timeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.timeout("1m");
    // end::verify-repository-request-timeout
    // tag::verify-repository-execute
    VerifyRepositoryResponse response = client.snapshot().verifyRepository(request, RequestOptions.DEFAULT);
    // end::verify-repository-execute
    // tag::verify-repository-response
    List<VerifyRepositoryResponse.NodeView> repositoryMetadataResponse = response.getNodes();
    // end::verify-repository-response
    assertThat(1, equalTo(repositoryMetadataResponse.size()));
    final boolean async = Booleans.parseBoolean(System.getProperty("tests.rest.async", "false"));
    if (async) {
        assertThat("asyncIntegTest-0", equalTo(repositoryMetadataResponse.get(0).getName()));
    } else {
        assertThat("integTest-0", equalTo(repositoryMetadataResponse.get(0).getName()));
    }
}
Also used : VerifyRepositoryResponse(org.opensearch.action.admin.cluster.repositories.verify.VerifyRepositoryResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) VerifyRepositoryRequest(org.opensearch.action.admin.cluster.repositories.verify.VerifyRepositoryRequest)

Example 74 with RestHighLevelClient

use of org.opensearch.client.RestHighLevelClient in project OpenSearch by opensearch-project.

the class SnapshotClientDocumentationIT method testSnapshotCreateRepository.

public void testSnapshotCreateRepository() throws IOException {
    RestHighLevelClient client = highLevelClient();
    // tag::create-repository-request
    PutRepositoryRequest request = new PutRepositoryRequest();
    // end::create-repository-request
    // tag::create-repository-create-settings
    String locationKey = FsRepository.LOCATION_SETTING.getKey();
    String locationValue = ".";
    String compressKey = FsRepository.COMPRESS_SETTING.getKey();
    boolean compressValue = true;
    Settings settings = Settings.builder().put(locationKey, locationValue).put(compressKey, compressValue).build();
    // end::create-repository-create-settings
    // tag::create-repository-request-repository-settings
    // <1>
    request.settings(settings);
    // end::create-repository-request-repository-settings
    {
        // tag::create-repository-settings-builder
        Settings.Builder settingsBuilder = Settings.builder().put(locationKey, locationValue).put(compressKey, compressValue);
        // <1>
        request.settings(settingsBuilder);
    // end::create-repository-settings-builder
    }
    {
        // tag::create-repository-settings-map
        Map<String, Object> map = new HashMap<>();
        map.put(locationKey, locationValue);
        map.put(compressKey, compressValue);
        // <1>
        request.settings(map);
    // end::create-repository-settings-map
    }
    {
        // tag::create-repository-settings-source
        request.settings("{\"location\": \".\", \"compress\": \"true\"}", // <1>
        XContentType.JSON);
    // end::create-repository-settings-source
    }
    // tag::create-repository-request-name
    // <1>
    request.name(repositoryName);
    // end::create-repository-request-name
    // tag::create-repository-request-type
    // <1>
    request.type(FsRepository.TYPE);
    // end::create-repository-request-type
    // tag::create-repository-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::create-repository-request-masterTimeout
    // tag::create-repository-request-timeout
    // <1>
    request.timeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.timeout("1m");
    // end::create-repository-request-timeout
    // tag::create-repository-request-verify
    // <1>
    request.verify(true);
    // end::create-repository-request-verify
    // tag::create-repository-execute
    AcknowledgedResponse response = client.snapshot().createRepository(request, RequestOptions.DEFAULT);
    // end::create-repository-execute
    // tag::create-repository-response
    // <1>
    boolean acknowledged = response.isAcknowledged();
    // end::create-repository-response
    assertTrue(acknowledged);
}
Also used : AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest) Map(java.util.Map) HashMap(java.util.HashMap) Settings(org.opensearch.common.settings.Settings)

Example 75 with RestHighLevelClient

use of org.opensearch.client.RestHighLevelClient in project OpenSearch by opensearch-project.

the class SnapshotClientDocumentationIT method testSnapshotDeleteSnapshot.

public void testSnapshotDeleteSnapshot() throws IOException {
    RestHighLevelClient client = highLevelClient();
    createTestRepositories();
    createTestIndex();
    createTestSnapshots();
    // tag::delete-snapshot-request
    DeleteSnapshotRequest request = new DeleteSnapshotRequest(repositoryName);
    request.snapshots(snapshotName);
    // end::delete-snapshot-request
    // tag::delete-snapshot-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::delete-snapshot-request-masterTimeout
    // tag::delete-snapshot-execute
    AcknowledgedResponse response = client.snapshot().delete(request, RequestOptions.DEFAULT);
    // end::delete-snapshot-execute
    // tag::delete-snapshot-response
    // <1>
    boolean acknowledged = response.isAcknowledged();
    // end::delete-snapshot-response
    assertTrue(acknowledged);
}
Also used : AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) DeleteSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.delete.DeleteSnapshotRequest)

Aggregations

RestHighLevelClient (org.opensearch.client.RestHighLevelClient)102 IOException (java.io.IOException)42 CountDownLatch (java.util.concurrent.CountDownLatch)42 ActionListener (org.opensearch.action.ActionListener)38 LatchedActionListener (org.opensearch.action.LatchedActionListener)38 OpenSearchException (org.opensearch.OpenSearchException)30 HashMap (java.util.HashMap)29 Map (java.util.Map)27 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)23 DefaultShardOperationFailedException (org.opensearch.action.support.DefaultShardOperationFailedException)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)16 Test (org.junit.Test)15 CreateIndexResponse (org.opensearch.client.indices.CreateIndexResponse)15 IndexRequest (org.opensearch.action.index.IndexRequest)13 Settings (org.opensearch.common.settings.Settings)13 SearchRequest (org.opensearch.action.search.SearchRequest)12 SearchResponse (org.opensearch.action.search.SearchResponse)12 HttpHost (org.apache.http.HttpHost)11 Request (org.opensearch.client.Request)10