Elasticsearch / SLS ES 只读数据分析
连接任意 ES 实例或阿里云 SLS ES 兼容端点,提供只读数据查询与分析。多 Profile 管理、索引探索、聚合分析、search_after 全量提取。零依赖。
FEATURES
支持多个 ES 集群 / SLS 端点配置,一键切换。凭据加密存储(权限 0600)。
拦截 DELETE/PUT 等写入操作,自动限制返回 200 条,密码不出现在命令行输出。
自动处理阿里云 SLS 的时间窗口限制、嵌套字段索引缺失、_source 过滤不可靠等坑。
列出索引、查看 mapping、采样数据。Agent 首次分析前读取作为上下文。
高效分页提取指定字段唯一值,自动 search_after,适合大数据量去重场景。
仅使用 Python 3 标准库(urllib + json),无需安装任何第三方包。
REFERENCE
多 Profile 连接管理
| 命令 | 说明 | 示例 |
|---|---|---|
add | 添加连接 | ./es config add prod --url https://es.example.com --user elastic --password *** --test |
list | 列出 Profile | ./es config list |
use | 切换默认 Profile | ./es config use staging |
test | 测试连接 | ./es config test prod |
remove | 删除 Profile | ./es config remove old |
只读查询与分析
| 命令 | 说明 | 示例 |
|---|---|---|
query | ES 查询 | ./es query '{"size":10,"query":{"match_all":{}}}' |
indices | 索引列表 | ./es indices --filter "logs*" |
mapping | 查看 Mapping | ./es mapping my-index |
count | 文档计数 | ./es count my-index |
sample | 采样数据 | ./es sample my-index -n 10 |
extract | 全量去重提取 | ./es extract my-index --field user_id |
对 Agent 说"查一下 ES 里昨天的 ERROR 日志",Agent 自动选择合适的查询:
> "查看 chat 索引里最近的错误日志"
Agent 执行:
./es query -i chat-2024.02 '{"size":20,"query":{"match":{"level":"ERROR"}},"sort":[{"@timestamp":"desc"}]}'需要导出大量数据时,使用 search_after 分页:
./es extract my-index --field auth_id --full-range -o /tmp/uids.csv| 策略 | 说明 |
|---|---|
| 只读拦截 | 拦截 DELETE / PUT(非 _search)/ POST(非 _search/_count/_mapping) |
| 行数限制 | 自动添加 size: 200 |
| 凭据安全 | 配置文件权限 0600,密码不出现在命令行输出 |
es (Bash CLI 入口)
│
├── config → config.py 多 Profile 连接管理
├── query → query.py ES 只读查询
├── indices → indices.py 索引列表
├── mapping → mapping.py Mapping 查看
├── count → count.py 文档计数
├── extract → extract.py 全量去重提取 (search_after)
└── sample → sample.py 采样数据
↓
es_core.py 核心引擎 (HTTP 连接 + 安全 + 输出格式化)
↓
~/.agents/data/es-analytics/
└── profiles.json 连接配置INSTALL