Elasticsearch 常用任务管理命令及实战应用

常用任务管理命令

  • 列出所有任务
shell 复制代码
curl -X GET "http://<es_host>:<es_port>/_tasks?detailed=true&pretty" -H 'Content-Type: application/json'
  • 获取特定类型的任务
shell 复制代码
curl -X GET "http://<es_host>:<es_port>/_tasks?actions=<action_type>" -H 'Content-Type: application/json'
  • 列出所有查询任务
shell 复制代码
curl -X GET "http://<es_host>:<es_port>/_tasks?detailed=true&actions=*search" -H 'Content-Type: application/json'
  • 取消所有查询任务
    如果 es 查询因大任务而卡住,可以临时采取此措施
shell 复制代码
curl -X POST "http://<es_host>:<es_port>/_tasks/_cancel?actions=*search" -H 'Content-Type: application/json'
  • 获取特定任务的详细信息
shell 复制代码
curl -X GET "http://<es_host>:<es_port>/_tasks/<task_id>" -H 'Content-Type: application/json'
  • 取消特定任务
shell 复制代码
curl -X POST "http://<es_host>:<es_port>/_tasks/_cancel?task_id=<task_id>" -H 'Content-Type: application/json'
  • 获取特定节点上的任务
shell 复制代码
curl -X GET "http://<es_host>:<es_port>/_tasks?nodes=<node_id>" -H 'Content-Type: application/json'

实战

定时检测 Elasticsearch 后台运行的查询任务,如果任务运行时间超过 59 秒,则进行企业微信群告警通知

python 复制代码
import requests
import time

# Elasticsearch节点的URL
es_url = "http://<es_user>:<es_pwd>@<es_host>:<es_port>/_tasks?detailed=true"

# 获取任务信息
response = requests.get(es_url)
tasks_data = response.json()

# 遍历节点和任务
for node_id, node_info in tasks_data.get('nodes', {}).items():
    for task_id, task_info in node_info.get('tasks', {}).items():
        running_time_seconds = task_info.get('running_time_in_nanos', 0) / 1e9
        description = task_info.get('description', '')
        
        if running_time_seconds > 59 and description:
            running_time_formatted = f"{running_time_seconds:.2f}"
            # 准备单个任务的Markdown内容
            content = (
                f"# 有大任务在 Elasticsearch 上运行\n"
                f"- **任务 ID**: {task_id}\n"
                f"  **查询语句**: {description}\n"
                f"  **运行时间**: {running_time_formatted} seconds\n"
            )

            # 发送到Webhook
            QYWX_BODY = {
                "msgtype": "markdown",
                "markdown": {
                    "content": content
                }
            }

            BOT_KEY = "xxxxxxxxxxxxxxxxx"  # 企业微信群 bot key

            webhook_url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={BOT_KEY}"
            headers = {'Content-Type': 'application/json; charset=utf-8'}

            response = requests.post(webhook_url, json=QYWX_BODY, headers=headers)

            # 检查响应状态
            if response.status_code == 200:
                print(f"Notification for Task ID {task_id} sent successfully.")
            else:
                print(f"Failed to send notification for Task ID {task_id}. Status code: {response.status_code}, Response: {response.text}")
            # 等待 2 秒
            time.sleep(2)

print("Processing completed.")
相关推荐
SelectDB技术团队9 小时前
冷热分层从建到验:Storage Policy 创建、表级与分区级绑定、SHOW TABLETS 验证的完整命令
大数据·数据库·elasticsearch·搜索引擎·数据分析·apache doris·冷热分层
Sayai12 小时前
DataX 离线数据同步实战:Elasticsearch 数据批量导出 MySQL(作业配置、并发调优与定时同步平台化设计)
大数据·mysql·elasticsearch·datax·数据同步
Elastic 中国社区官方博客13 小时前
Kibana:创建包含时间序列图表的仪表板
大数据·elasticsearch·搜索引擎·信息可视化·全文检索·kibana
Elastic 中国社区官方博客16 小时前
Elastic 线下 Meetup 将于 2026 年 12 月 19 号下午在西安举行
大数据·数据库·elasticsearch·搜索引擎·ai·数据分析·全文检索
SelectDB技术团队16 小时前
存算分离建表与 Storage Vault 配置全流程:日志集群从建库到弹性扩缩容的命令链
elasticsearch·日志·数据湖·apache doris·日志数据·数据库选型·多模湖仓
闻缺陷则喜何志丹17 小时前
软件需求说明书应包括的内容
任务
今年下半年18 小时前
【Spring Boot】集成 Elasticsearch:中文分词与关键字高亮对接文档
spring boot·elasticsearch·中文分词
jonyleek18 小时前
JVS私有化文档系统运维避坑指南:5大断点检测与加固实践(K8S+MinIO+ES)
elasticsearch·minio·clamav·jvs·k8s运维·文档系统·权限同步
骑着蜗牛撵大象32718 小时前
CentOS 7 单机拉起 Elasticsearch 7.17.29:从内核报错到公网可达的全链路记录
elasticsearch·centos·jenkins·restful·内核调优·公网部署
打工仔折腾 AI18 小时前
CentOS 7 单机部署 Elasticsearch 7.17.29:启动报错排查与 CRUD 验证
人工智能·后端·python·elasticsearch·性能优化·centos·jenkins