如何获取已完成ElasticSearch任务的执行结果?
获取已完成任务结果的解决方案
问题背景
- 任务管理API仅返回当前运行中的任务数据,需获取已完成任务的结果
- 尝试查询
.tasks索引时触发404错误,报错信息:{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index [.tasks]","resource.type":"index_or_alias","resource.id":".tasks","index_uuid":"na","index":".tasks"}],"type":"index_not_found_exception","reason":"no such index [.tasks]","resource.type":"index_or_alias","resource.id":".tasks","index_uuid":"na","index":".tasks"},"status":404} - 执行
GET _cat/indices/.*?v后,集群中无.tasks索引,当前存在的系统索引包括.kibana_account_name、.kibana_XXXX、.opensearch-notebooks、.kibana_1、.opendistro_security
解决方案
从索引列表能看出,你的集群是**OpenSearch(原OpenDistro)**而非原生Elasticsearch,两者在任务持久化实现上存在差异:
开启任务持久化配置
OpenSearch默认不保存已完成任务,需修改opensearch.yml配置文件,添加或调整以下参数:opendistro.task.index.enabled: true opendistro.task.index.interval: 5m opendistro.task.index.expiry_days: 7opendistro.task.index.enabled:开启已完成任务的索引存储功能opendistro.task.index.interval:设置批量写入已完成任务的时间间隔opendistro.task.index.expiry_days:设置任务数据的保留天数
重启集群使配置生效
修改配置后重启OpenSearch节点,集群会自动创建.opendistro-tasks索引(注意不是Elasticsearch的.tasks索引)查询已完成任务
配置生效且运行一段时间后,可通过以下命令查询所有已完成任务:GET .opendistro-tasks/_search { "query": { "match_all": {} } }也可按任务ID、类型等条件过滤查询:
GET .opendistro-tasks/_search { "query": { "term": { "task_id": "你的任务ID" } } }临时方案:实时跟踪任务结果
若无法修改集群配置,提交异步任务时添加wait_for_completion=false参数获取任务ID,之后用以下命令轮询任务状态,直至任务完成即可获取结果:GET _tasks/{task_id}?wait_for_completion=false
内容的提问来源于stack exchange,提问作者Shambala
相关产品推荐
相关产品推荐

