'How to know if a reindex from remote task is sucessful or failed in elasticsearch using the java high level rest API

I'm using the ElasticSeach highlevel rest api client to do some custom reindexing from another cluster.

    ReindexRequest reindexRequest = new ReindexRequest()
        .setMaxDocs(3000)
        .setDestIndex("my-new-index")
        .setTimeout(TimeValue.timeValueHours(1))
        .setRemoteInfo(
            new RemoteInfo("http", "otherhost", 9200, "/",
                new BytesArray(selectQuery.toString()), "user",
                "password",
                Map.of()), TimeValue.timeValueSeconds(30),
                TimeValue.timeValueSeconds(30)))
        .setSourceIndices("old-index")
        .setScript(new Script(ScriptType.STORED, null, "add_sc_id", Map.of("sc_id", some-id)));

      TaskSubmissionResponse task = esClient.submitReindexTask(reindexRequest, RequestOptions.DEFAULT);

I periodically check the task to see if it's done or not using the task API

Optional<GetTaskResponse> getTaskResponse =
            esClient.tasks().get(new GetTaskRequest(nodeId, taskId), RequestOptions.DEFAULT);

Using this, I can see if the task is completed with getTaskResponse.get().isCompleted() but I don't see anyway to check if it's successful or not.

By doing the GET _taks/nodeId:taskId with curl, I see there is a response.failures field.

Is there a way to retrieve this field with the Java High level rest api client? Or is there another way to achieve this?



Solution 1:[1]

Please check List Task API.

You can use below Java code for geeting task Failure information:

ListTasksRequest request = new ListTasksRequest();
request.setActions("cluster:*"); 
request.setNodes("nodeId1", "nodeId2"); 
request.setParentTaskId(new TaskId("parentTaskId", 42)); 
ListTasksResponse responseTask = client.tasks().list(request,RequestOptions.DEFAULT);
List<TaskOperationFailure> taskFailures = response.getTaskFailures();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sagar Patel