--- title: "查询指定任务" date: 2026-03-21 lastmod: 2026-03-21 description: "介绍如何在 Easysearch 中查询指定任务的详细信息和状态。" tags: ["任务管理", "异步操作", "监控"] summary: "此 API 用于获取集群中指定任务的详细信息。 API # GET /_tasks/{task_id} API 的作用 # 返回集群中指定任务的详细信息和当前状态。任务是在集群中运行的任何操作,例如搜索、索引、重建索引等。通过任务 ID,可以获取特定任务的: 任务状态:当前运行状态 执行时间:任务开始时间和运行时长 任务类型:任务的类型和操作 可取消性:任务是否可以被取消 执行节点:任务执行所在的节点信息 API 的参数 # 路由参数 # 参数 类型 是否必需 描述 task_id string 必需 任务 ID 格式:node_id:task_number 例如:Mgqdm0f9SEGClWxp_RdnaQ:17416 node_id:节点的唯一标识符 task_number:节点内任务的数字 ID 查询字符串参数 # 参数 类型 是否必需 默认值 描述 wait_for_completion boolean 可选 false 是否等待任务完成" --- 此 API 用于获取集群中指定任务的详细信息。 ## API ``` GET /_tasks/{task_id} ``` ## API 的作用 返回集群中指定任务的详细信息和当前状态。任务是在集群中运行的任何操作,例如搜索、索引、重建索引等。通过任务 ID,可以获取特定任务的: - **任务状态**:当前运行状态 - **执行时间**:任务开始时间和运行时长 - **任务类型**:任务的类型和操作 - **可取消性**:任务是否可以被取消 - **执行节点**:任务执行所在的节点信息 ## API 的参数 ### 路由参数 | 参数 | 类型 | 是否必需 | 描述 | |------|------|----------|------| | `task_id` | string | 必需 | 任务 ID
格式:`node_id:task_number`
例如:`Mgqdm0f9SEGClWxp_RdnaQ:17416`
`node_id`:节点的唯一标识符
`task_number`:节点内任务的数字 ID | ### 查询字符串参数 | 参数 | 类型 | 是否必需 | 默认值 | 描述 | |------|------|----------|--------|------| | `wait_for_completion` | boolean | 可选 | false | 是否等待任务完成
`true`:阻塞直到任务完成
`false`:立即返回当前状态 | | `timeout` | time | 可选 | 无 | 操作超时时间
当 `wait_for_completion=true` 时有效
格式:`30s`, `1m`, `5m` 等 | ## 请求示例 ```bash # 查询指定任务的当前状态 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 ``` ## 响应示例 ### 成功响应 ```json { "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": {} } } } } } ``` ### 任务已完成的响应 ```json { "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` | 主机地址 | | `ip` | IP 地址和端口 | | `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` 关联请求与任务 ## 任务状态说明 任务状态取决于任务类型,常见状态包括: | 任务类型 | 状态字段示例 | |----------|-------------| | Reindex | `total`, `updated`, `created`, `deleted`, `batches` | | Delete By Query | `total`, `deleted`, `batches`, `version_conflicts` | | Update By Query | `total`, `updated`, `version_conflicts` | ## 注意事项 1. 此 API 只支持 GET 方法 2. 任务 ID 必须符合 `node_id:task_number` 格式 3. 如果任务已完成且结果已存储,API 会返回完成后的任务信息 4. `wait_for_completion=true` 时会阻塞请求直到任务完成或超时 5. 任务完成后,任务信息会被存储一段时间供查询 ## 相关文档 - [查询任务列表](./get-task-list.md) - [取消任务](./cancel-task.md) - [创建快照](./create-snapshot.md) - [升级索引](./upgrade-index.md)