ElasticSearch索引命令

前言

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存储数据

相关推荐
和裕2 小时前
光伏储能定制加强型瓦楞纸箱:降低组件运输隐裂率与售后损耗的核心价值
大数据·运维·网络·人工智能·算法
智购科技无人售货机工厂店2 小时前
2026自动售货机大屏交互设计原则:从信息层级到视觉动线的工程实践~YH
运维·python·单片机·嵌入式硬件·自动化·交互
玉宇夕落2 小时前
背单词项目一:融会贯通:从零构建"单词大师"后台——Next.js + Supabase + Drizzle 全栈开发实战
后端
橙序员小站2 小时前
从 Demo 到生产:Agent Harness 如何把 AI 真正“接”进项目
后端·aigc·ai编程
Ruiery2 小时前
Linux 6.6内核 PCIe 深度解析(十):PCIe Port Driver(portdrv)— 一个物理 Port,拆出五个逻辑服务
linux·运维·服务器
她的男孩2 小时前
一个开源低代码框架的协作 SPI 是怎么设计的 ForgeAdmin 拆解 + 实战接入新平台
后端·算法·架构
蓝山6162 小时前
Python 字典(Dictionary)完全指南:从入门到实战
后端
foggyprojects3 小时前
在 DeepSeek Harness 里跑通 Foggy:从安装插件到第一次问数
后端
不甘先生3 小时前
Go 中 type、方法与指针接收者:从 str_name.Name() 看懂 Go 的类型系统
开发语言·后端·golang