Search in sources :

Example 1 with TaskSubmissionResponse

use of org.opensearch.client.tasks.TaskSubmissionResponse in project OpenSearch by opensearch-project.

the class ReindexIT method testReindexTask.

public void testReindexTask() throws Exception {
    final String sourceIndex = "source123";
    final String destinationIndex = "dest2";
    {
        // Prepare
        Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build();
        createIndex(sourceIndex, settings);
        createIndex(destinationIndex, settings);
        BulkRequest bulkRequest = new BulkRequest().add(new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)).add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        assertEquals(RestStatus.OK, highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status());
    }
    {
        // tag::submit-reindex-task
        // <1>
        ReindexRequest reindexRequest = new ReindexRequest();
        reindexRequest.setSourceIndices(sourceIndex);
        reindexRequest.setDestIndex(destinationIndex);
        reindexRequest.setRefresh(true);
        TaskSubmissionResponse reindexSubmission = highLevelClient().submitReindexTask(reindexRequest, // <2>
        RequestOptions.DEFAULT);
        // <3>
        String taskId = reindexSubmission.getTask();
        // end::submit-reindex-task
        assertBusy(checkTaskCompletionStatus(client(), taskId));
    }
}
Also used : ReindexRequest(org.opensearch.index.reindex.ReindexRequest) TaskSubmissionResponse(org.opensearch.client.tasks.TaskSubmissionResponse) BulkRequest(org.opensearch.action.bulk.BulkRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexRequest(org.opensearch.action.index.IndexRequest) Settings(org.opensearch.common.settings.Settings)

Example 2 with TaskSubmissionResponse

use of org.opensearch.client.tasks.TaskSubmissionResponse in project OpenSearch by opensearch-project.

the class TasksIT method testGetValidTask.

public void testGetValidTask() throws Exception {
    // Run a Reindex to create a task
    final String sourceIndex = "source1";
    final String destinationIndex = "dest";
    Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build();
    createIndex(sourceIndex, settings);
    createIndex(destinationIndex, settings);
    BulkRequest bulkRequest = new BulkRequest().add(new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)).add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)).setRefreshPolicy(RefreshPolicy.IMMEDIATE);
    assertEquals(RestStatus.OK, highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status());
    final ReindexRequest reindexRequest = new ReindexRequest().setSourceIndices(sourceIndex).setDestIndex(destinationIndex);
    final TaskSubmissionResponse taskSubmissionResponse = highLevelClient().submitReindexTask(reindexRequest, RequestOptions.DEFAULT);
    final String taskId = taskSubmissionResponse.getTask();
    assertNotNull(taskId);
    TaskId childTaskId = new TaskId(taskId);
    GetTaskRequest gtr = new GetTaskRequest(childTaskId.getNodeId(), childTaskId.getId());
    gtr.setWaitForCompletion(randomBoolean());
    Optional<GetTaskResponse> getTaskResponse = execute(gtr, highLevelClient().tasks()::get, highLevelClient().tasks()::getAsync);
    assertTrue(getTaskResponse.isPresent());
    GetTaskResponse taskResponse = getTaskResponse.get();
    if (gtr.getWaitForCompletion()) {
        assertTrue(taskResponse.isCompleted());
    }
    org.opensearch.tasks.TaskInfo info = taskResponse.getTaskInfo();
    assertTrue(info.isCancellable());
    assertEquals("reindex from [source1] to [dest]", info.getDescription());
    assertEquals("indices:data/write/reindex", info.getAction());
    if (taskResponse.isCompleted() == false) {
        assertBusy(checkTaskCompletionStatus(client(), taskId));
    }
}
Also used : GetTaskRequest(org.opensearch.client.tasks.GetTaskRequest) TaskSubmissionResponse(org.opensearch.client.tasks.TaskSubmissionResponse) TaskId(org.opensearch.client.tasks.TaskId) GetTaskResponse(org.opensearch.client.tasks.GetTaskResponse) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) Settings(org.opensearch.common.settings.Settings)

Aggregations

BulkRequest (org.opensearch.action.bulk.BulkRequest)2 IndexRequest (org.opensearch.action.index.IndexRequest)2 TaskSubmissionResponse (org.opensearch.client.tasks.TaskSubmissionResponse)2 Settings (org.opensearch.common.settings.Settings)2 ReindexRequest (org.opensearch.index.reindex.ReindexRequest)2 Matchers.containsString (org.hamcrest.Matchers.containsString)1 GetTaskRequest (org.opensearch.client.tasks.GetTaskRequest)1 GetTaskResponse (org.opensearch.client.tasks.GetTaskResponse)1 TaskId (org.opensearch.client.tasks.TaskId)1