You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ElasticSearch重新索引任务:获取任务ID后如何追踪任务状态?

Tracking Elasticsearch Reindex Task Status

Since you’ve already grabbed the task ID (elasticSearchReIndexTaskId) from your asynchronous reindex request, you can use the NEST client’s GetTask method to check the task’s progress and status at any time. Here’s a step-by-step breakdown of how to implement this:

Basic Status Check Code

// Fetch the task status using your stored task ID
var taskResponse = client.GetTask(new GetTaskRequest(elasticSearchReIndexTaskId));

if (taskResponse.IsValid)
{
    var task = taskResponse.Task;
    
    // Check if the task is still in progress
    if (!task.Completed)
    {
        var reindexStatus = task.Status?.Reindex;
        Console.WriteLine($"Task is running: {reindexStatus?.Created} docs created out of {reindexStatus?.Total} total.");
    }
    else
    {
        Console.WriteLine("Reindex task finished!");
        var reindexStatus = task.Status?.Reindex;
        
        // Print detailed completion metrics
        if (reindexStatus != null)
        {
            Console.WriteLine($"Total docs processed: {reindexStatus.Total}");
            Console.WriteLine($"Successfully created: {reindexStatus.Created}");
            Console.WriteLine($"Skipped docs: {reindexStatus.Skipped}");
            Console.WriteLine($"Failed docs: {reindexStatus.Failed}");
        }
        
        // Handle any errors if the task failed
        if (task.Error != null)
        {
            Console.WriteLine($"Task failed with error: {task.Error.Reason}");
        }
    }
}
else
{
    Console.WriteLine($"Failed to retrieve task status: {taskResponse.ServerError?.Error?.Reason}");
}

Key Properties to Monitor

  • task.Completed: A boolean flag that tells you if the task has finished running.
  • task.Status.Reindex: Contains real-time progress metrics specific to reindex operations, including total documents, successful creations, skips, and failures.
  • task.Error: If the task encountered a problem, this field will hold the error message and details.

Polling for Completion (Optional)

If you need to wait for the task to finish before proceeding with other operations, you can set up a simple polling loop:

while (true)
{
    var taskResponse = client.GetTask(new GetTaskRequest(elasticSearchReIndexTaskId));
    
    // Exit loop if task is completed or we can't fetch status
    if (!taskResponse.IsValid || taskResponse.Task.Completed)
    {
        break;
    }
    
    var progress = taskResponse.Task.Status?.Reindex;
    Console.WriteLine($"Waiting for completion... {progress?.Created}/{progress?.Total} docs done");
    Thread.Sleep(5000); // Wait 5 seconds between checks
}

// Final status check after loop ends
var finalTask = client.GetTask(new GetTaskRequest(elasticSearchReIndexTaskId)).Task;
if (finalTask.Completed && finalTask.Error == null)
{
    Console.WriteLine("Reindex completed successfully!");
}
else if (finalTask.Error != null)
{
    Console.WriteLine($"Reindex failed: {finalTask.Error.Reason}");
}

Important Notes

  • Ensure your Elasticsearch user has the monitor or manage cluster privileges to access task statuses.
  • Elasticsearch automatically cleans up old tasks after a period (configurable via task.index.expiry). If you check a completed task long after it finished, it might no longer be available in the task list—you can verify completion by checking the document count in the destination index instead.

内容的提问来源于stack exchange,提问作者hyankov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:44:32