In Elasticsearch (ES), you can run a task and check its status with the task API.
For example, if you use the reindex
API in ES with parameter with_for_completion=False
,
ES will not wait for the process to finish and will return a task instead.
Check running tasks#
To check the running tasks, we can use:
GET _tasks
This will return a list of running tasks.
You can also specify the query parameters, for example, to filter based on reindex
action, you can use:
GET _tasks?action=*reindex*
The above API will return response in JSON format. If you do not want to parse the result and just want to get human readable output, you can use cat task API:
GET _cat/tasks?action=*cluster*
Get status of a task#
If you know the task id of a task, you can check its detailed status using the get task API:
GET _tasks/<your-task-id>
It will show you if the task is completed, how long does it takes, and how many documents are processed etc.
Cancel a task#
To cancel a task, we can use the following API:
POST _tasks/<task-id>/_cancel
Note this is a POST
request instead of GET
.
ref:
- cancel a task: https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html#task-cancellation
Check info about finished tasks?#
Currently there is no API to check finished tasks, unless you know the task id (use the get task API above to check task details).
However, all task information is stored in a special index .tasks
.
We can query this index just like a normal index1. For example, you can do this:
GET .tasks/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"completed": true
}
},
{
"term": {
"task.action": "indices:data/write/reindex"
}
}
]
}
}
}
The above query will filter tasks based on completion status and action type.
References#
- View completed tasks: https://stackoverflow.com/q/58125914/6064933
We can do this for now, but in a future major version, direct access to system indices will be prevented by default, as suggested by Elasticsearch ↩︎