前言 Elasticsearch作为一个强大的搜索引擎,提供了多种方式来查看和管理数据。本文将详细介绍各种ES数据查看和管理工具,包括Kibana、Elasticsearch Head、curl命令、Postman等,帮助您高效地操作和管理ES数据。
1. 工具概览 1.1 常用工具对比
工具
类型
优点
缺点
推荐度
Kibana
官方可视化平台
功能强大、官方支持
资源占用大
⭐⭐⭐⭐⭐
Elasticsearch Head
浏览器插件/独立应用
轻量、直观
功能相对简单
⭐⭐⭐⭐
curl/HTTPie
命令行工具
灵活、脚本化
不够直观
⭐⭐⭐⭐
Postman
API测试工具
易用、可保存请求
需要手动配置
⭐⭐⭐⭐
Cerebro
Web管理工具
轻量、功能全
需要额外部署
⭐⭐⭐
ElasticHQ
Web管理工具
界面美观
更新较慢
⭐⭐⭐
Dejavu
数据浏览器
类似数据库客户端
功能单一
⭐⭐⭐
1.2 选择建议 生产环境推荐:
Kibana(官方、功能最全)
Cerebro(轻量级管理)
开发环境推荐:
Kibana Dev Tools
Elasticsearch Head
Postman
快速查看:
2. Kibana(官方推荐) 2.1 安装Kibana Docker安装(推荐):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 docker pull kibana:8.11.0 docker run -d \ --name kibana \ --net elastic \ -p 5601:5601 \ -e "ELASTICSEARCH_HOSTS=http://elasticsearch:9200" \ -e "I18N_LOCALE=zh-CN" \ kibana:8.11.0
传统安装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 wget https://artifacts.elastic.co/downloads/kibana/kibana-8.11.0-linux-x86_64.tar.gz tar -xzf kibana-8.11.0-linux-x86_64.tar.gz cd kibana-8.11.0vim config/kibana.yml server.port: 5601 server.host: "0.0.0.0" elasticsearch.hosts: ["http://localhost:9200" ] i18n.locale: "zh-CN" ./bin/kibana
访问路径:
1 2 Kibana首页 → Management → Dev Tools 或直接访问:http://localhost:5601/app/dev_tools#/console
基本操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 GET /_cluster/health GET /_cat/indices?v GET /user_index/_mapping GET /user_index/_search { "query" : { "match_all" : {} } } GET /user_index/_search { "query" : { "match" : { "name" : "张三" } } } POST /user_index/_doc/1 { "name" : "张三" , "age" : 25, "email" : "zhangsan@example.com" } POST /user_index/_update/1 { "doc" : { "age" : 26 } } DELETE /user_index/_doc/1 POST /_bulk {"index" :{"_index" :"user_index" ,"_id" :"1" }} {"name" :"张三" ,"age" :25} {"index" :{"_index" :"user_index" ,"_id" :"2" }} {"name" :"李四" ,"age" :30}
快捷键:
1 2 3 4 Ctrl + Enter:执行当前请求 Ctrl + /:注释/取消注释 Ctrl + I:自动格式化 Ctrl + Space:自动补全
2.3 Kibana Discover(数据探索) 访问路径:
1 Kibana首页 → Analytics → Discover
功能介绍:
创建索引模式(Index Pattern)
1 2 3 Management → Stack Management → Index Patterns → Create index pattern 输入:user_index 选择时间字段:@timestamp(如果有)
查看数据
时间范围选择:右上角选择时间范围
搜索栏:使用KQL(Kibana Query Language)或Lucene语法
字段过滤:左侧选择要显示的字段
保存查询:保存常用查询
KQL查询示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 name: "张三" name: 张* age >= 20 and age <= 30 name: "张三" and age > 25 email: * not status: "deleted"
查看文档详情
点击文档左侧的箭头展开
查看JSON格式或表格格式
查看周围文档(上下文)
2.4 Kibana Visualize(数据可视化) 创建可视化图表:
饼图(Pie Chart)
1 2 3 4 5 用途:展示分类占比 示例:用户年龄分布 配置: - Metrics: Count - Buckets: Terms aggregation on age field
柱状图(Bar Chart)
1 2 3 4 5 用途:对比不同类别的数据 示例:每月订单数量 配置: - Y-axis: Count - X-axis: Date Histogram on createTime
折线图(Line Chart)
1 2 3 4 5 用途:展示趋势变化 示例:用户增长趋势 配置: - Y-axis: Count - X-axis: Date Histogram
数据表(Data Table)
1 2 3 4 5 用途:展示详细数据 示例:商品销售排行 配置: - Metrics: Sum of sales - Buckets: Terms on product_name
2.5 Kibana Dashboard(仪表板) 创建仪表板:
1 2 3 4 5 1. 创建多个可视化图表 2. Dashboard → Create dashboard 3. 添加已创建的可视化图表 4. 调整布局和大小 5. 保存仪表板
仪表板功能:
实时刷新:自动刷新数据
时间过滤:统一的时间范围选择
交互过滤:点击图表进行过滤
导出:导出为PDF或PNG
2.6 Kibana Stack Monitoring(监控) 查看集群状态:
1 2 3 4 5 6 7 8 Management → Stack Monitoring 监控内容: - 集群健康状态 - 节点状态 - 索引状态 - 查询性能 - 索引性能
3. Elasticsearch Head 3.1 安装方式 方式1:Chrome插件(最简单)
1 2 3 4 1. 打开Chrome应用商店 2. 搜索"Elasticsearch Head" 3. 安装插件 4. 点击插件图标,输入ES地址:http://localhost:9200
方式2:Docker安装
1 2 3 4 5 6 docker run -d \ --name es-head \ -p 9100:9100 \ mobz/elasticsearch-head:5
方式3:源码安装
1 2 3 4 5 6 7 8 9 10 11 git clone https://github.com/mobz/elasticsearch-head.git cd elasticsearch-headnpm install npm run start
配置ES允许跨域:
1 2 3 http.cors.enabled: true http.cors.allow-origin: "*"
3.2 主要功能 1. 概览(Overview)
1 2 3 4 - 集群健康状态(绿/黄/红) - 节点列表 - 索引列表 - 分片分布
2. 索引(Indices)
1 2 3 4 - 查看所有索引 - 索引的文档数量 - 索引大小 - 分片信息
3. 浏览器(Browser)
1 2 3 - 浏览索引数据 - 查看文档详情 - 简单的搜索功能
4. 结构化查询(Structured Query)
1 2 3 - 可视化查询构建器 - 支持基本的查询条件 - 查看查询结果
5. 基本查询(Basic Query)
1 2 3 - 输入JSON格式的查询 - 执行查询 - 查看结果
3.3 使用示例 查看索引数据:
1 2 3 4 1. 点击"数据浏览"标签 2. 选择索引 3. 点击"浏览"按钮 4. 查看文档列表
执行查询:
1 2 3 4 5 6 7 8 9 10 1. 点击"复合查询"标签 2. 输入查询条件 { "query": { "match": { "name": "张三" } } } 3. 点击"搜索"按钮
4. curl命令行工具 4.1 基本语法 1 curl -X<METHOD> "http://localhost:9200/<PATH>" -H 'Content-Type: application/json' -d'<BODY>'
4.2 常用操作 1. 集群和索引管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 curl -X GET "http://localhost:9200/_cluster/health?pretty" curl -X GET "http://localhost:9200/_cat/indices?v" curl -X GET "http://localhost:9200/user_index/_mapping?pretty" curl -X GET "http://localhost:9200/user_index/_settings?pretty" curl -X PUT "http://localhost:9200/user_index" -H 'Content-Type: application/json' -d' { "settings": { "number_of_shards": 3, "number_of_replicas": 1 }, "mappings": { "properties": { "name": { "type": "text" }, "age": { "type": "integer" } } } } ' curl -X DELETE "http://localhost:9200/user_index"
2. 文档操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 curl -X PUT "http://localhost:9200/user_index/_doc/1" -H 'Content-Type: application/json' -d' { "name": "张三", "age": 25, "email": "zhangsan@example.com" } ' curl -X POST "http://localhost:9200/user_index/_doc" -H 'Content-Type: application/json' -d' { "name": "李四", "age": 30 } ' curl -X GET "http://localhost:9200/user_index/_doc/1?pretty" curl -X POST "http://localhost:9200/user_index/_update/1" -H 'Content-Type: application/json' -d' { "doc": { "age": 26 } } ' curl -X DELETE "http://localhost:9200/user_index/_doc/1"
3. 搜索操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 curl -X GET "http://localhost:9200/user_index/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } ' curl -X GET "http://localhost:9200/user_index/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match": { "name": "张三" } } } ' curl -X GET "http://localhost:9200/user_index/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "bool": { "must": [ { "match": { "name": "张三" } } ], "filter": [ { "range": { "age": { "gte": 20, "lte": 30 } } } ] } } } ' curl -X GET "http://localhost:9200/user_index/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "from": 0, "size": 10 } ' curl -X GET "http://localhost:9200/user_index/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "sort": [ { "age": "desc" } ] } '
4. 批量操作
1 2 3 4 5 6 7 8 9 10 curl -X POST "http://localhost:9200/_bulk" -H 'Content-Type: application/json' -d' {"index":{"_index":"user_index","_id":"1"}} {"name":"张三","age":25} {"index":{"_index":"user_index","_id":"2"}} {"name":"李四","age":30} {"update":{"_index":"user_index","_id":"1"}} {"doc":{"age":26}} {"delete":{"_index":"user_index","_id":"3"}} '
5. 聚合查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 curl -X GET "http://localhost:9200/user_index/_search?pretty" -H 'Content-Type: application/json' -d' { "size": 0, "aggs": { "avg_age": { "avg": { "field": "age" } }, "max_age": { "max": { "field": "age" } } } } ' curl -X GET "http://localhost:9200/user_index/_search?pretty" -H 'Content-Type: application/json' -d' { "size": 0, "aggs": { "group_by_age": { "terms": { "field": "age" } } } } '
4.3 curl技巧 保存查询到文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 cat > query.json << EOF { "query": { "match": { "name": "张三" } } } EOF curl -X GET "http://localhost:9200/user_index/_search?pretty" \ -H 'Content-Type: application/json' \ -d @query.json
格式化输出:
1 2 3 4 5 curl -X GET "http://localhost:9200/user_index/_search" | jq '.' curl -X GET "http://localhost:9200/user_index/_search" | jq '.hits.hits[]._source'
添加认证:
1 2 3 4 5 6 curl -u elastic:password -X GET "http://localhost:9200/_cluster/health?pretty" curl -H "Authorization: Basic ZWxhc3RpYzpwYXNzd29yZA==" \ -X GET "http://localhost:9200/_cluster/health?pretty"
5. HTTPie(更友好的curl) 5.1 安装HTTPie 1 2 3 4 5 6 7 8 brew install httpie sudo apt install httpiepip install httpie
5.2 基本使用 语法更简洁:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 http GET localhost:9200/_cluster/health http POST localhost:9200/user_index/_doc/1 \ name="张三" \ age:=25 \ email="zhangsan@example.com" http PUT localhost:9200/user_index http DELETE localhost:9200/user_index/_doc/1
查询示例:
1 2 3 4 5 6 7 8 9 10 http POST localhost:9200/user_index/_search \ query:='{"match_all":{}}' http POST localhost:9200/user_index/_search \ query:='{"match":{"name":"张三"}}' http POST localhost:9200/user_index/_search < query.json
6. Postman 6.1 配置Postman 创建ES环境:
1 2 3 4 5 6 1. 点击右上角的"Environment" 2. 创建新环境"Elasticsearch" 3. 添加变量: - es_host: http://localhost:9200 - username: elastic - password: password
配置认证:
1 2 3 1. 在请求的Authorization标签 2. 选择"Basic Auth" 3. 输入用户名和密码
6.2 创建请求集合 集群管理:
1 2 3 GET {{es_host}}/_cluster/health GET {{es_host}}/_cat/indices?v GET {{es_host}}/_cat/nodes?v
索引操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # 创建索引 PUT {{es_host}}/user_index Content-Type: application/json { "settings": { "number_of_shards": 3 }, "mappings": { "properties": { "name": { "type": "text" }, "age": { "type": "integer" } } } } # 查看索引 GET {{es_host}}/user_index # 删除索引 DELETE {{es_host}}/user_index
文档操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 # 创建文档 PUT {{es_host}}/user_index/_doc/1 Content-Type: application/json { "name": "张三", "age": 25, "email": "zhangsan@example.com" } # 查询文档 GET {{es_host}}/user_index/_doc/1 # 搜索文档 POST {{es_host}}/user_index/_search Content-Type: application/json { "query": { "match": { "name": "张三" } } }
6.3 Postman技巧 使用变量:
1 2 3 4 5 6 7 pm.environment .set ("timestamp" , new Date ().toISOString ()); { "createTime" : "{{timestamp}}" }
测试脚本:
1 2 3 4 5 6 7 8 9 pm.test ("Status code is 200" , function ( ) { pm.response .to .have .status (200 ); }); pm.test ("Response has data" , function ( ) { var jsonData = pm.response .json (); pm.expect (jsonData.hits .total .value ).to .be .above (0 ); });
导出/导入集合:
1 2 3 4 1. 右键集合 → Export 2. 保存为JSON文件 3. 分享给团队成员 4. Import导入集合
7. Cerebro(轻量级管理工具) 7.1 安装Cerebro Docker安装:
1 2 3 4 5 6 docker run -d \ --name cerebro \ -p 9000:9000 \ lmenezes/cerebro:latest
传统安装:
1 2 3 4 5 6 7 8 9 10 11 wget https://github.com/lmenezes/cerebro/releases/download/v0.9.4/cerebro-0.9.4.tgz tar -xzf cerebro-0.9.4.tgz cd cerebro-0.9.4./bin/cerebro
7.2 主要功能 1. 集群概览
1 2 3 4 - 集群健康状态 - 节点信息 - 索引列表 - 分片分布可视化
2. 索引管理
1 2 3 4 5 - 创建/删除索引 - 打开/关闭索引 - 刷新索引 - 清空缓存 - 优化索引
3. 节点管理
1 2 3 - 查看节点详情 - 节点统计信息 - 节点配置
4. REST请求
5. 快照管理
8. 其他工具 8.1 ElasticHQ Docker安装:
1 2 3 4 5 6 docker run -d \ --name elastichq \ -p 5000:5000 \ elastichq/elasticsearch-hq
功能特点:
美观的UI界面
集群监控
索引管理
查询工具
节点管理
8.2 Dejavu Docker安装:
1 2 3 4 5 6 docker run -d \ --name dejavu \ -p 1358:1358 \ appbaseio/dejavu
功能特点:
类似数据库客户端的界面
可视化数据浏览
数据编辑
导入/导出数据
实时搜索
8.3 Elasticvue 浏览器插件:
1 2 3 1. Chrome/Firefox应用商店搜索"Elasticvue" 2. 安装插件 3. 点击插件图标连接ES
功能特点:
现代化UI
索引管理
文档浏览和编辑
查询构建器
快照管理
9. 实用脚本 9.1 批量导入数据 Shell脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash ES_HOST="http://localhost:9200" INDEX_NAME="user_index" for i in {1..1000}do echo "{\"index\":{\"_index\":\"$INDEX_NAME \",\"_id\":\"$i \"}}" echo "{\"name\":\"用户$i \",\"age\":$((20 + RANDOM % 40) ),\"email\":\"user$i @example.com\"}" done | curl -s -H "Content-Type: application/x-ndjson" -XPOST "$ES_HOST /_bulk" --data-binary @-echo "导入完成"
Python脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 from elasticsearch import Elasticsearchfrom elasticsearch.helpers import bulkes = Elasticsearch(['http://localhost:9200' ]) def generate_data (count ): for i in range (1 , count + 1 ): yield { "_index" : "user_index" , "_id" : i, "_source" : { "name" : f"用户{i} " , "age" : 20 + (i % 40 ), "email" : f"user{i} @example.com" } } success, failed = bulk(es, generate_data(1000 )) print (f"成功: {success} , 失败: {failed} " )
9.2 数据导出 导出为JSON:
1 2 3 4 5 6 7 8 9 10 11 12 13 #!/bin/bash ES_HOST="http://localhost:9200" INDEX_NAME="user_index" OUTPUT_FILE="export.json" curl -X POST "$ES_HOST /$INDEX_NAME /_search?scroll=1m" \ -H 'Content-Type: application/json' \ -d '{"size":1000,"query":{"match_all":{}}}' \ | jq '.hits.hits[]._source' > $OUTPUT_FILE echo "导出完成: $OUTPUT_FILE "
Python导出脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 from elasticsearch import Elasticsearchimport jsones = Elasticsearch(['http://localhost:9200' ]) from elasticsearch.helpers import scanquery = {"query" : {"match_all" : {}}} results = scan(es, index="user_index" , query=query) with open ('export.json' , 'w' , encoding='utf-8' ) as f: for hit in results: json.dump(hit['_source' ], f, ensure_ascii=False ) f.write('\n' ) print ("导出完成" )
9.3 索引备份和恢复 创建快照:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/bin/bash ES_HOST="http://localhost:9200" curl -X PUT "$ES_HOST /_snapshot/my_backup" -H 'Content-Type: application/json' -d' { "type": "fs", "settings": { "location": "/mount/backups/my_backup" } } ' curl -X PUT "$ES_HOST /_snapshot/my_backup/snapshot_$(date +%Y%m%d) " -H 'Content-Type: application/json' -d' { "indices": "user_index,product_index", "ignore_unavailable": true, "include_global_state": false } ' echo "快照创建完成"
恢复快照:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #!/bin/bash ES_HOST="http://localhost:9200" SNAPSHOT_NAME="snapshot_20251210" curl -X POST "$ES_HOST /_snapshot/my_backup/$SNAPSHOT_NAME /_restore" -H 'Content-Type: application/json' -d' { "indices": "user_index,product_index", "ignore_unavailable": true, "include_global_state": false } ' echo "快照恢复完成"
9.4 监控脚本 集群健康监控:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/bin/bash ES_HOST="http://localhost:9200" while true ; do health=$(curl -s "$ES_HOST /_cluster/health" | jq -r '.status' ) nodes=$(curl -s "$ES_HOST /_cluster/health" | jq '.number_of_nodes' ) indices=$(curl -s "$ES_HOST /_cat/indices" | wc -l) echo "$(date) - 状态: $health , 节点: $nodes , 索引: $indices " if [ "$health " != "green" ]; then echo "警告: 集群状态异常!" fi sleep 60 done
10. 最佳实践 10.1 日常操作建议 1. 使用合适的工具
1 2 3 4 5 开发调试:Kibana Dev Tools 数据浏览:Kibana Discover 可视化:Kibana Dashboard 快速查看:Elasticsearch Head 脚本操作:curl/Python
2. 查询优化
1 2 3 4 - 使用filter代替query(不需要评分时) - 限制返回字段(_source) - 合理设置分页大小 - 使用scroll API处理大量数据
3. 安全建议
1 2 3 4 - 启用认证和授权 - 使用HTTPS - 限制网络访问 - 定期备份数据
10.2 常见问题排查 1. 连接失败
1 2 3 4 5 6 7 8 curl http://localhost:9200 netstat -an | grep 9200 docker logs elasticsearch
2. 查询慢
1 2 3 4 5 6 7 8 9 GET /_cat/indices?v&s=search.query_time_in_millis:desc GET /user_index/_search { "profile" : true , "query" : { "match" : { "name" : "张三" } } }
3. 磁盘空间不足
1 2 3 4 5 6 7 8 GET /_cat/allocation?v DELETE /old_index_* POST /_cache/clear
10.3 监控指标 关键指标:
1 2 3 4 5 6 7 8 1. 集群健康状态(green/yellow/red) 2. 节点状态 3. 索引数量和大小 4. 查询响应时间 5. 索引速度 6. CPU和内存使用率 7. 磁盘使用率 8. JVM堆内存使用
监控命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 GET /_cluster/health GET /_nodes/stats GET /_stats GET /_cat/thread_pool?v GET /_tasks
11. 总结 11.1 工具选择指南
场景
推荐工具
原因
开发调试
Kibana Dev Tools
功能强大,语法高亮
数据浏览
Kibana Discover
强大的搜索和过滤
可视化分析
Kibana Dashboard
官方支持,功能全面
快速查看
Elasticsearch Head
轻量,直观
脚本操作
curl/Python
自动化,灵活
API测试
Postman
易用,可保存
集群管理
Cerebro
轻量,功能全
11.2 学习路径
入门阶段
学习curl基本命令
使用Elasticsearch Head浏览数据
了解基本的CRUD操作
进阶阶段
掌握Kibana Dev Tools
学习复杂查询DSL
使用Kibana Discover探索数据
高级阶段
创建Kibana可视化和仪表板
编写自动化脚本
集群监控和优化
11.3 推荐资源 官方文档:
工具下载:
社区资源:
参考资源