🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Task Management API 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.0/tasks.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/tasks.html) 译文链接 : [http://www.apache.wiki/display/Elasticsearch/Task+Management+API](http://www.apache.wiki/display/Elasticsearch/Task+Management+API) 贡献者 : [那伊抹微笑](/display/~wangyangting) 警告 : 该 Task Management API 是一个新功能,并且仍然处于实验阶段,该 API 可能会改变方式并且不向后兼容。 ## Current Tasks Information(当前任务信息) **task management API** 可以获取关于集群中一个或多个节点正在执行中的任务的信息。 ``` curl -XGET 'localhost:9200/_tasks ?pretty'① curl -XGET 'localhost:9200/_tasks?nodes=nodeId1,nodeId2 &pretty'② curl -XGET 'localhost:9200/_tasks?nodes=nodeId1,nodeId2&actions=cluster:* &pretty'③ ``` | 编号 | 描述 | | --- | --- | | [![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/tasks.html#CO135-1) | Retrieves all tasks currently running on all nodes in the cluster. | | [![](https://img.kancloud.cn/50/46/50466ab92768bfee2306a5da100c7496_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/tasks.html#CO135-2) | Retrieves all tasks running on nodes `nodeId1` and `nodeId2`. See [the section called “Node specification](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/cluster.html#cluster-nodes "Node specificationedit")[edit](https://github.com/elastic/elasticsearch/edit/5.0/docs/reference/cluster.asciidoc "Edit this page on GitHub")” for more info about how to select individual nodes. | | [![](https://img.kancloud.cn/59/09/59090687855ebb833c063fe09e622609_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/tasks.html#CO135-3) | Retrieves all cluster-related tasks running on nodes `nodeId1` and `nodeId2`. | 返回结果看起来如下 :  ``` { "nodes" : { "oTUltX4IQMOUUVeiohTt8A" : { "name" : "H5dfFeA", "transport_address" : "127.0.0.1:9300", "host" : "127.0.0.1", "ip" : "127.0.0.1:9300", "tasks" : { "oTUltX4IQMOUUVeiohTt8A:124" : { "node" : "oTUltX4IQMOUUVeiohTt8A", "id" : 124, "type" : "direct", "action" : "cluster:monitor/tasks/lists[n]", "start_time_in_millis" : 1458585884904, "running_time_in_nanos" : 47402, "cancellable" : false, "parent_task_id" : "oTUltX4IQMOUUVeiohTt8A:123" }, "oTUltX4IQMOUUVeiohTt8A:123" : { "node" : "oTUltX4IQMOUUVeiohTt8A", "id" : 123, "type" : "transport", "action" : "cluster:monitor/tasks/lists", "start_time_in_millis" : 1458585884904, "running_time_in_nanos" : 236042, "cancellable" : false } } } } } ``` 也可以获取指定任务的信息 :  ``` curl -XGET 'localhost:9200/_tasks/task_id:1 ?pretty' ``` | [![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/tasks.html#CO136-1) | This will return a 404 if the task isn’t found. | 或者获取任务所有子任务的信息 :  ``` curl -XGET 'localhost:9200/_tasks?parent_task_id=parentTaskId:1 &pretty' ``` 如果父任务不存在也不会返回 404 错误。 该任务 **API** 也可用于等待一个指定的任务完成。在 **id** 为 **oTUltX4IQMOUUVeiohTt8A** 的任务完成之前,下面的调用将锁住 **10** 秒。 ``` curl -XGET 'localhost:9200/_tasks/oTUltX4IQMOUUVeiohTt8A:12345?wait_for_completion=true&timeout=10s&pretty' ``` 你可以等待所有任务中的某个动作类型直到完成。这个命令将等待所有的 **reindex** 任务直到完成 :  ``` curl -XGET 'localhost:9200/_tasks?actions=*reindex&wait_for_completion=true&timeout=10s&pretty' ``` 可以使用 **_cat** 列出任务命令列表,它接受与标准的 **list task** 命令相同的参数。 ``` curl -XGET 'localhost:9200/_cat/tasks?pretty' ``` ## Task Cancellation(任务取消) 如果一个长期运行的任务支持取消,可以通过下列的命令来取消它 :  ``` curl -XPOST 'localhost:9200/_tasks/task_id:1/_cancel?pretty' ``` 该任务取消命令支持和 **list tasks** 命令相同的任务选择参数,所以多个任务可以在同时取消。例如,下面的命令将取消所有运行在 **nodeId1** 和 **nodeId2** 上的 **reindex** 任务。 ``` curl -XPOST 'localhost:9200/_tasks/_cancel?nodes=nodeId1,nodeId2&actions=*reindex&pretty' ``` ## Task Grouping(任务组) 通过使用 **task API** 命令使用 **group_by** 参数可以通过 **nodes**(默认)或者父任务来分组以返回任务列表。下面的命令将改变分组为父任务 :  ``` curl -XGET 'localhost:9200/_tasks?group_by=parents&pretty' ```