📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

此 API 用于获取集群中指定任务的详细信息。

API #

GET /_tasks/{task_id}

API 的作用 #

返回集群中指定任务的详细信息和当前状态。任务是在集群中运行的任何操作,例如搜索、索引、重建索引等。通过任务 ID,可以获取特定任务的:

  • 任务状态:当前运行状态
  • 执行时间:任务开始时间和运行时长
  • 任务类型:任务的类型和操作
  • 可取消性:任务是否可以被取消
  • 执行节点:任务执行所在的节点信息

API 的参数 #

路由参数 #

参数类型是否必需描述
task_idstring必需任务 ID
格式:node_id:task_number
例如:Mgqdm0f9SEGClWxp_RdnaQ:17416
node_id:节点的唯一标识符
task_number:节点内任务的数字 ID

查询字符串参数 #

参数类型是否必需默认值描述
wait_for_completionboolean可选false是否等待任务完成
true:阻塞直到任务完成
false:立即返回当前状态
timeouttime可选操作超时时间
wait_for_completion=true 时有效
格式:30s, 1m, 5m

请求示例 #

# 查询指定任务的当前状态
GET /_tasks/Mgqdm0f9SEGClWxp_RdnaQ:17416

# 等待任务完成
GET /_tasks/Mgqdm0f9SEGClWxp_RdnaQ:17416?wait_for_completion=true

# 设置等待超时时间
GET /_tasks/Mgqdm0f9SEGClWxp_RdnaQ:17416?wait_for_completion=true&timeout=5m

响应示例 #

成功响应 #

{
  "nodes": {
    "Mgqdm0f9SEGClWxp_RdnaQ": {
      "name": "data-node-1",
      "transport_address": "192.168.1.10:9300",
      "host": "192.168.1.10",
      "ip": "192.168.1.10:9300",
      "roles": ["data", "ingest", "master"],
      "tasks": {
        "Mgqdm0f9SEGClWxp_RdnaQ:17416": {
          "node": "Mgqdm0f9SEGClWxp_RdnaQ",
          "id": 17416,
          "type": "transport",
          "action": "indices:data/write/reindex",
          "status": {
            "total": 10000,
            "updated": 5000,
            "created": 0,
            "deleted": 0,
            "batches": 5,
            "version_conflicts": 0,
            "noops": 0,
            "retries": {
              "bulk": 0,
              "search": 0
            },
            "throttled_millis": 0,
            "requests_per_second": -1,
            "throttled_until_millis": 0
          },
          "description": "reindex from [source-index] to [target-index]",
          "running_time_in_nanos": 15234567890,
          "cancellable": true,
          "headers": {}
        }
      }
    }
  }
}

任务已完成的响应 #

{
  "completed": true,
  "task": {
    "node": "Mgqdm0f9SEGClWxp_RdnaQ",
    "id": 17416,
    "type": "transport",
    "action": "indices:data/write/reindex",
    "status": {
      "total": 10000,
      "updated": 10000,
      "created": 0,
      "deleted": 0
    },
    "description": "reindex from [source-index] to [target-index]",
    "running_time_in_nanos": 52345678901,
    "cancellable": false
  },
  "response": {
    "took": 52345,
    "timed_out": false,
    "total": 10000,
    "updated": 10000,
    "created": 0,
    "deleted": 0,
    "batches": 10,
    "version_conflicts": 0,
    "failures": []
  }
}

响应字段说明 #

节点信息字段 #

字段描述
name节点名称
transport_address传输层地址
host主机地址
ipIP 地址和端口
roles节点角色列表

任务信息字段 #

字段描述
node执行任务的节点 ID
id任务数字 ID
type任务类型
action任务操作
status任务状态信息(因任务类型而异)
description任务描述
running_time_in_nanos任务运行时长(纳秒)
cancellable任务是否可取消
headers请求头信息

使用场景 #

  1. 任务监控:监控长时间运行任务的进度
  2. 问题排查:诊断任务执行中的问题
  3. 性能分析:分析任务的执行时间和资源消耗
  4. 异步操作:等待异步任务完成并获取结果

获取任务 ID #

任务 ID 可以从以下途径获取:

  1. 创建任务时返回:某些 API(如 reindex)会在响应中返回 task_id
  2. 查询所有任务:使用 GET /_tasks 获取所有任务列表
  3. X-Opaque-Id 头:通过请求头 X-Opaque-Id 关联请求与任务

任务状态说明 #

任务状态取决于任务类型,常见状态包括:

任务类型状态字段示例
Reindextotal, updated, created, deleted, batches
Delete By Querytotal, deleted, batches, version_conflicts
Update By Querytotal, updated, version_conflicts

注意事项 #

  1. 此 API 只支持 GET 方法
  2. 任务 ID 必须符合 node_id:task_number 格式
  3. 如果任务已完成且结果已存储,API 会返回完成后的任务信息
  4. wait_for_completion=true 时会阻塞请求直到任务完成或超时
  5. 任务完成后,任务信息会被存储一段时间供查询

相关文档 #