前言
ElasticSearch命令
es命令
以下是 Elasticsearch 在 Kibana Dev Tools 中最常用的查询命令速查手册,按使用场景分类:
一、集群与节点健康
json
编辑
bash
1# 集群健康状态
2GET _cluster/health
3
4# 节点信息
5GET _cat/nodes?v
6
7# 集群统计
8GET _cluster/stats
二、索引管理
json
编辑
perl
1# 查看所有索引
2GET _cat/indices?v
3
4# 创建索引
5PUT /my-index
6{
7 "settings": { "number_of_shards": 1, "number_of_replicas": 0 },
8 "mappings": {
9 "properties": {
10 "title": { "type": "text" },
11 "price": { "type": "float" },
12 "created": { "type": "date" }
13 }
14 }
15}
16
17# 删除索引
18DELETE /my-index
19
20# 查看索引映射结构
21GET /my-index/_mapping
22
23# 查看索引设置
24GET /my-index/_settings
三、文档 CRUD
json
编辑
perl
1# 写入文档(指定 ID)
2PUT /my-index/_doc/1
3{ "title": "Elasticsearch 入门", "price": 99.0 }
4
5# 写入文档(自动生成 ID)
6POST /my-index/_doc
7{ "title": "Kibana 实战", "price": 128.0 }
8
9# 根据 ID 查询
10GET /my-index/_doc/1
11
12# 更新文档(部分更新)
13POST /my-index/_update/1
14{ "doc": { "price": 88.0 } }
15
16# 删除文档
17DELETE /my-index/_doc/1
18
19# 批量操作
20POST _bulk
21{ "index": { "_index": "my-index", "_id": "2" } }
22{ "title": "批量写入示例", "price": 50.0 }
23{ "delete": { "_index": "my-index", "_id": "1" } }
四、核心查询 DSL(_search)
1. 匹配所有
json
编辑
perl
1GET /my-index/_search
2{ "query": { "match_all": {} } }
2. 全文匹配(match)
json
编辑
perl
1GET /my-index/_search
2{ "query": { "match": { "title": "elasticsearch 入门" } } }
3. 精确匹配(term)
⚠️ 用于
keyword、数字、日期等不分词字段
json
编辑
perl
1GET /my-index/_search
2{ "query": { "term": { "status": "published" } } }
4. 多条件组合(bool)
json
编辑
perl
1GET /my-index/_search
2{
3 "query": {
4 "bool": {
5 "must": [ { "match": { "title": "elasticsearch" } } ],
6 "filter": [ { "range": { "price": { "gte": 50, "lte": 200 } } } ],
7 "must_not": [ { "term": { "status": "draft" } } ],
8 "should": [ { "term": { "tag": "hot" } } ]
9 }
10 }
11}
must:必须满足(影响评分)filter:必须满足(不评分,有缓存,性能优先用这个)must_not:必须不满足should:可选满足
5. 范围查询(range)
json
编辑
perl
1GET /my-index/_search
2{
3 "query": {
4 "range": {
5 "created": { "gte": "2026-01-01", "lte": "now", "format": "yyyy-MM-dd" }
6 }
7 }
8}
6. 通配符 / 前缀
json
编辑
perl
1GET /my-index/_search
2{ "query": { "prefix": { "title.keyword": "Elastic" } } }
3
4GET /my-index/_search
5{ "query": { "wildcard": { "title.keyword": "Elastic*" } } }
7. 多字段查询(multi_match)
json
编辑
perl
1GET /my-index/_search
2{ "query": { "multi_match": { "query": "搜索关键词", "fields": ["title", "content"] } } }
五、分页、排序、指定返回字段
json
编辑
perl
1GET /my-index/_search
2{
3 "from": 0,
4 "size": 10,
5 "sort": [ { "price": "desc" }, { "created": "asc" } ],
6 "_source": ["title", "price"],
7 "query": { "match_all": {} }
8}
六、聚合分析(Aggregations)
1. 分组计数(terms)
json
编辑
perl
1GET /my-index/_search
2{
3 "size": 0,
4 "aggs": {
5 "by_status": { "terms": { "field": "status.keyword" } }
6 }
7}
2. 数值统计(avg/max/min/sum)
json
编辑
perl
1GET /my-index/_search
2{
3 "size": 0,
4 "aggs": {
5 "avg_price": { "avg": { "field": "price" } },
6 "max_price": { "max": { "field": "price" } }
7 }
8}
3. 日期直方图(按时间分桶)
json
编辑
perl
1GET /my-index/_search
2{
3 "size": 0,
4 "aggs": {
5 "sales_over_time": {
6 "date_histogram": { "field": "created", "calendar_interval": "month" }
7 }
8 }
9}
4. 嵌套聚合
json
编辑
perl
1GET /my-index/_search
2{
3 "size": 0,
4 "aggs": {
5 "by_status": {
6 "terms": { "field": "status.keyword" },
7 "aggs": { "avg_price": { "avg": { "field": "price" } } }
8 }
9 }
10}
七、高亮、建议、计数
json
编辑
perl
1# 高亮
2GET /my-index/_search
3{
4 "query": { "match": { "title": "elasticsearch" } },
5 "highlight": { "fields": { "title": {} } }
6}
7
8# 仅统计数量(不返回文档)
9GET /my-index/_count
10{ "query": { "match": { "title": "elasticsearch" } } }
11
12# 自动补全建议
13GET /my-index/_search
14{ "suggest": { "my-suggest": { "text": "elas", "term": { "field": "title.keyword" } } } }
八、常用 _cat 速查命令
| 命令 | 用途 |
|---|---|
GET _cat/health?v |
集群健康 |
GET _cat/nodes?v |
节点列表 |
GET _cat/indices?v |
索引列表 |
GET _cat/shards?v |
分片分布 |
GET _cat/allocation?v |
磁盘分配 |
GET _cat/templates?v |
索引模板 |
GET _cat/aliases?v |
别名列表 |
GET _cat/tasks?v |
当前任务 |
总结
可以使用es存储数据