--- title: "查询任务列表" date: 2026-01-11 lastmod: 2026-01-11 description: "查询集群中当前运行的任务" tags: ["任务", "查询", "监控"] summary: "获取集群中当前正在运行的任务信息。 API 格式 # GET /_tasks GET /_tasks/{task_id} API 作用 # 该 API 用于检索集群中当前正在运行的任务信息: 查看所有节点的运行任务 按节点、父任务或平铺方式分组显示 过滤特定节点、动作或父任务 等待任务完成后再返回结果 检查任务是否可取消 API 参数 # 路径参数 # 参数 类型 是否必填 默认值 描述 {task_id} String 否 - 任务 ID(格式:node_id:task_number)。指定后仅返回该任务信息 查询参数 # 参数 类型 是否必填 默认值 描述 nodes String 否 所有节点 逗号分隔的节点 ID 或名称列表。使用 _local 仅返回连接节点的信息 actions String 否 所有动作 逗号分隔的动作掩码列表,用于过滤返回的动作 detailed Boolean 否 false 是否返回详细任务信息(包含状态信息) parent_task_id String 否 所有任务 按父任务 ID 过滤(格式:node_id:task_number)。设为 -1 或留空返回所有任务 wait_for_completion Boolean 否 false 是否等待所有找到的任务完成 group_by String 否 nodes 任务分组方式:nodes(按节点)、parents(按父任务)、none(平铺) timeout Time 否 30s 显式操作超时时间 master_timeout Time 否 30s 等待主节点连接的超时时间 请求示例 # 查询所有任务 # GET /_tasks 查询指定任务 # GET /_tasks/node1:123 查询特定节点的任务 # GET /_tasks?" --- 获取集群中当前正在运行的任务信息。 ## API 格式 ``` GET /_tasks GET /_tasks/{task_id} ``` ## API 作用 该 API 用于检索集群中当前正在运行的任务信息: - 查看所有节点的运行任务 - 按节点、父任务或平铺方式分组显示 - 过滤特定节点、动作或父任务 - 等待任务完成后再返回结果 - 检查任务是否可取消 ## API 参数 ### 路径参数 | 参数 | 类型 | 是否必填 | 默认值 | 描述 | |------|------|----------|--------|------| | `{task_id}` | String | 否 | - | 任务 ID(格式:`node_id:task_number`)。指定后仅返回该任务信息 | ### 查询参数 | 参数 | 类型 | 是否必填 | 默认值 | 描述 | |------|------|----------|--------|------| | `nodes` | String | 否 | 所有节点 | 逗号分隔的节点 ID 或名称列表。使用 `_local` 仅返回连接节点的信息 | | `actions` | String | 否 | 所有动作 | 逗号分隔的动作掩码列表,用于过滤返回的动作 | | `detailed` | Boolean | 否 | `false` | 是否返回详细任务信息(包含状态信息) | | `parent_task_id` | String | 否 | 所有任务 | 按父任务 ID 过滤(格式:`node_id:task_number`)。设为 `-1` 或留空返回所有任务 | | `wait_for_completion` | Boolean | 否 | `false` | 是否等待所有找到的任务完成 | | `group_by` | String | 否 | `nodes` | 任务分组方式:`nodes`(按节点)、`parents`(按父任务)、`none`(平铺) | | `timeout` | Time | 否 | `30s` | 显式操作超时时间 | | `master_timeout` | Time | 否 | `30s` | 等待主节点连接的超时时间 | ## 请求示例 ### 查询所有任务 ``` GET /_tasks ``` ### 查询指定任务 ``` GET /_tasks/node1:123 ``` ### 查询特定节点的任务 ``` GET /_tasks?nodes=node1,node2 ``` ### 详细信息模式 ``` GET /_tasks?detailed=true ``` ### 按父任务分组 ``` GET /_tasks?group_by=parents ``` ### 等待任务完成 ``` GET /_tasks?wait_for_completion=true&timeout=10m ``` ## 响应示例 ### 默认响应(group_by=nodes) ```json { "nodes": { "node1": { "name": "node-1", "transport_address": "192.168.1.10:9300", "host": "node-1.example.com", "ip": "192.168.1.10:9300", "roles": ["data", "ingest", "master"], "attributes": { "attr1": "value1" }, "tasks": { "node1:123": { "node": "node1", "id": 123, "type": "transport", "action": "indices:data/write/index", "description": "index[{index1}], type[_doc], id[AVx7XlYqBYs1B8mWEqJl]", "start_time_in_millis": 1738665600000, "running_time_in_nanos": 1500000000, "running_time": "1.5s", "cancellable": true, "headers": {} }, "node1:124": { "node": "node1", "id": 124, "type": "transport", "action": "indices:data/read/search", "description": "search[query]", "start_time_in_millis": 1738665601000, "running_time_in_nanos": 500000000, "running_time": "500ms", "cancellable": true, "parent_task_id": "node1:100", "headers": {} } } }, "node2": { "name": "node-2", "transport_address": "192.168.1.11:9300", "host": "node-2.example.com", "ip": "192.168.1.11:9300", "roles": ["data", "master"], "attributes": {}, "tasks": { "node2:456": { "node": "node2", "id": 456, "type": "transport", "action": "cluster:monitor/health", "description": "cluster health", "start_time_in_millis": 1738665602000, "running_time_in_nanos": 300000000, "running_time": "300ms", "cancellable": false, "headers": {} } } } } } ``` ### 详细模式响应(detailed=true) ```json { "nodes": { "node1": { "name": "node-1", "transport_address": "192.168.1.10:9300", "host": "node-1.example.com", "ip": "192.168.1.10:9300", "roles": ["data"], "tasks": { "node1:123": { "node": "node1", "id": 123, "type": "transport", "action": "indices:data/write/index", "description": "index[{index1}]", "status": "running", "start_time_in_millis": 1738665600000, "running_time_in_nanos": 1500000000, "running_time": "1.5s", "cancellable": true, "headers": {} } } } } } ``` ### 平铺模式响应(group_by=none) ```json { "tasks": [ { "node": "node1", "id": 123, "type": "transport", "action": "indices:data/write/index", "description": "index[{index1}]", "start_time_in_millis": 1738665600000, "running_time_in_nanos": 1500000000, "running_time": "1.5s", "cancellable": true } ] } ``` ## 响应字段说明 ### 节点对象字段 | 字段 | 类型 | 描述 | |------|------|------| | `name` | String | 节点名称 | | `transport_address` | String | 传输地址(host:port) | | `host` | String | 主机名 | | `ip` | String | IP 地址 | | `roles` | String[] | 节点角色列表 | | `attributes` | Object | 节点属性 | | `tasks` | Object | 该节点上的任务对象 | ### 任务对象字段 | 字段 | 类型 | 描述 | |------|------|------| | `node` | String | 任务运行的节点 ID | | `id` | Integer | 数字任务 ID | | `type` | String | 任务类型(通常为 "transport") | | `action` | String | 正在执行的动作名称 | | `description` | String | 可读的任务描述 | | `status` | String | 当前任务状态(仅当 detailed=true) | | `start_time_in_millis` | Long | 任务开始时间(毫秒时间戳) | | `running_time_in_nanos` | Long | 任务运行时长(纳秒) | | `running_time` | String | 可读的运行时长 | | `cancellable` | Boolean | 任务是否可取消 | | `parent_task_id` | String | 父任务 ID(如果存在) | | `headers` | Object | 任务特定的头信息 | ## 常见动作类型 | 动作 | 描述 | |------|------| | `indices:data/write/index` | 索引文档 | | `indices:data/read/search` | 搜索操作 | | `cluster:monitor/health` | 集群健康检查 | | `cluster:update_settings` | 更新集群设置 | | `indices:admin/create` | 创建索引 | | `indices:admin/delete` | 删除索引 | ## 使用场景 1. **任务监控**:查看集群中正在运行的任务 2. **性能分析**:识别长时间运行的任务 3. **故障排查**:检查任务状态和依赖关系 4. **资源管理**:了解节点负载和任务分布 ## 注意事项 1. **任务 ID 格式**:任务 ID 格式为 `node_id:task_number` 2. **可取消性**:并非所有任务都支持取消操作 3. **性能影响**:detailed=true 可能会增加响应时间 4. **分组方式**:group_by 参数影响响应结构 5. **超时设置**:长时间运行的任务可能需要增加超时时间 ## 相关操作 - **POST /_tasks/{task_id}/_cancel**:取消任务 - **GET /_cat/tasks**:以表格格式查看任务 - **GET /_tasks/{task_id}**:查询特定任务 ## 实现文件 - **REST 处理器**:`RestListTasksAction.java` - **Transport Action**:`TransportListTasksAction.java` - **请求类**:`ListTasksRequest.java` - **响应类**:`ListTasksResponse.java` - **任务信息**:`TaskInfo.java`