文章目录
- ES 索引相关操作
-
- 一、分片(Sharding)原理
-
- 集群健康状态(Health Status)
- 为什么 ES 只能修改副本数量,不能修改分片数量?
- 二、索引管理(Index Management)
-
- 2.1 常用操作速查
- 2.2 创建索引
- 2.3 查看索引信息
- 2.4 修改索引配置
- 2.5 修改索引字段(Mapping)
- 2.6 查看索引列表
- 2.7 删除索引
- 2.8 索引别名(Alias)
- 三、Mapping 数据类型
-
- 数字类型
- 字符串类型
- 日期类型
- boolean 类型
- 二进制数据
- 多字段特性
- 数组类型
- IP 类型
- 四、文档操作(Document CRUD)
-
- 4.1 创建文档
- 4.2 查询文档
- 4.3 修改文档
- 4.4 删除文档
- 4.5 清空文档
- 五、复杂查询(Complex Query)
-
- 5.1 简单查询(term 精确匹配)
- 5.2 过滤查询(filter)
- 5.3 多条件复合查询(must)
- 5.4 terms 多值查询(类似 MySQL IN)
- 5.5 should 嵌套查询
- 六、集群状态异常(Red)处理
ES 索引相关操作
本文覆盖 ES 索引的管理(含别名)、Mapping 数据类型、文档 CRUD 以及常用查询语法。
一、分片(Sharding)原理
数据会按照哈希规则被切分到多个主分片(Primary Shard),每个主分片可以配置若干个副本分片(Replica Shard),副本用于容灾并提升读取性能。
示例:数据 abc 被切分为 5 个分片(a、b、c、d、e),副本数量为 1。
| 主分片 | A | B | C | D | E |
分布在 3 个节点上:
| node01 | a | C |
| node02 | d、b | E、A |
| node03 | e、c | D、B |
亮色代表主分片,暗色代表副分片
![[05_中间件/ELK/image/Pasted image 20260704110934.png]]
集群健康状态(Health Status)
ES 通过集群健康检查返回三种状态:
| Green(绿色) | 所有主分片和副本分片都已分配,集群完全健康。 |
| Yellow(黄色) | 所有主分片已分配,但至少有一个副本分片未分配,数据完整但存在单点风险。 |
| Red(红色) | 至少有一个主分片未分配,部分数据不可用,集群无法正常提供完整服务。 |
为什么 ES 只能修改副本数量,不能修改分片数量?
核心原因是文档路由机制。
ES 通过公式 shard = hash(_routing) % 主分片数 决定一篇文档存储在哪个主分片上。主分片数一旦改变,取模结果全部变化,所有文档的归属分片都会错乱,相当于要把整个索引的数据重新洗牌,代价极大且无法在线平滑完成。
而副本分片只是主分片的"拷贝",增减副本不影响文档路由,只是多复制或少复制几份数据,所以可以随时通过 number_of_replicas 动态调整。
补充:ES 提供了 Split API(增加分片)和 Shrink API(减少分片),但本质都是创建一个新索引、重新迁移数据,并非原地直接修改。
二、索引管理(Index Management)
2.1 常用操作速查
| PUT | localhost:9200/索引名称/类型名称/文档id | 创建文档(指定文档 id) |
| POST | localhost:9200/索引名称/类型名称 | 创建文档(随机文档 id) |
| POST | localhost:9200/索引名称/类型名称/文档id/_update | 修改文档 |
| DELETE | localhost:9200/索引名称/类型名称/文档id | 删除文档 |
| GET | localhost:9200/索引名称/类型名称/文档id | 通过文档 id 查询文档 |
| POST | localhost:9200/索引名称/类型名称/_search | 查询所有数据 |
说明:7.x 及以上版本建议使用固定类型名 _doc,例如 my_index/_doc/1。
2.2 创建索引
创建索引时可同时指定 settings(分片 / 副本参数)和 mappings(字段结构):
curl -X PUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
"settings": { # 索引参数设置
"number_of_shards": 3, # 分片数量,默认 1(7.x 之前默认 5)
"number_of_replicas": 2 # 副本数量,默认 1
},
"mappings": { # 字段配置(类比数据库表结构;ES 也可先写入文档再自动生成 mapping)
"properties": {
"id": { "type": "integer" }, # 字段名 + 数据类型
"name": { "type": "keyword" }
}
}
}
'
![[05_中间件/ELK/image/Pasted image 20260704110850.png]]
2.3 查看索引信息
# 查看索引设置(?pretty 用于美化输出,可省略)
curl -XGET 'localhost:9200/my_index/_settings?pretty'
# 查看索引映射
curl -XGET 'localhost:9200/my_index/_mapping?pretty'
2.4 修改索引配置
curl -XPUT 'localhost:9200/my_index/_settings?pretty' -H 'Content-Type: application/json' -d '{"number_of_replicas": 1}'
2.5 修改索引字段(Mapping)
注意:只能新增字段,不能删除或修改已有字段。
curl -XPOST 'localhost:9200/my_index/_mapping?pretty' -H 'Content-Type: application/json' -d'
{
"properties": {
"age": { "type": "short" }
}
}
'
2.6 查看索引列表
curl -XGET 'localhost:9200/_cat/indices?v'
2.7 删除索引
curl -XDELETE 'localhost:9200/my_index?pretty'
[[05_中间件/ELK/image/5dc5d5eefc9cfff9e9ee0511df4f166c_MD5.jpg|Open: Pasted image 20260813103007.png]] ![[05_中间件/ELK/image/5dc5d5eefc9cfff9e9ee0511df4f166c_MD5.jpg]]
2.8 索引别名(Alias)
别名(alias)相当于给索引起的“外号”:业务代码只面向别名,切换索引时只需修改别名的指向,代码不用动,是 ES 里做零停机重建索引(reindex)的常用手段。
# 创建索引别名
curl -XPOST 'localhost:9200/_aliases?pretty' -H 'Content-Type: application/json' -d '
{
"actions": [
{ "add": { "index": "my_index", "alias": "my_index_alias" } }
]
}
'
# 删除索引别名:把 add 改成 remove,其余结构一致
curl -XPOST 'localhost:9200/_aliases?pretty' -H 'Content-Type: application/json' -d '
{
"actions": [
{ "remove": { "index": "my_index", "alias": "my_index_alias" } }
]
}
'
技巧:创建别名用 add,删除别名只需把 add 改成 remove。
[[05_中间件/ELK/image/b5d736ee303f5dd6eb28ff3487cbabd3_MD5.jpg|Open: Pasted image 20260813114158.png]] ![[05_中间件/ELK/image/b5d736ee303f5dd6eb28ff3487cbabd3_MD5.jpg]]
三、Mapping 数据类型
数字类型
- 整型:byte(-128 ~ 127)、short(-32768 ~ 32767)、integer(-2^31 ~ 231-1)、`long`(-263 ~ 2^63-1)
- 浮点型:double、float、half_float、scaled_float(以整数形式表示小数,需配合 scaling_factor 指明精度)
字符串类型
- text:适用于被全文搜索的字段(如摘要、产品描述)。会被分词器分词,存入倒排索引后可全文检索,但很少用于排序和聚合分析。
- keyword:适用于结构化字段(如 email 地址、主机名、状态码、标签)。不分词,只能精确查询,无法全文检索,常用来做过滤、排序和聚合分析。
日期类型
- date:ES 没有固定的日期类型,只要符合日期格式即可被视作日期,如格式化日期字符串、表示秒/毫秒的整数等。可通过 format 指定多种格式,例如 "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"。
boolean 类型
- true:true 或 "true"
- false:false 或 "false"
二进制数据
- binary:用于存储 Base64 编码的字符串,且字符串中不能包含换行符。
多字段特性
可对一个字段添加多个子字段:
"title": {
"type": "text",
"fields": {
"desc": { "type": "keyword" } # desc 为子字段名,可自定义
}
}
数组类型
一个字段可包含多个值,但需要保证所有值的数据类型一致:
{
"ids": [1, 2],
"tags": ["one", "two"],
"users": [ { "name": "Mary", "age": 12 }, { "name": "John", "age": 10 } ]
}
IP 类型
- ip:用于存储 IPv4 或 IPv6 地址。
四、文档操作(Document CRUD)
版本说明:ES 7.0 起废弃了 type(类型)概念,8.0 已彻底移除。下文示例均使用 7.x / 8.x 的 _doc 写法(索引名/_doc/文档ID);若使用 5.x 及更早版本,需要在索引名后加上 <类型名称>。
4.1 创建文档
PUT 必须指定文档 id;POST 可不指定,不指定时自动生成随机 id。
# 指定文档 id
curl -XPUT 'localhost:9200/my_index/_doc/1?pretty' -H 'Content-Type: application/json' -d'
{
"id": 101,
"name": "zhangsan",
"age": 18
}
'
# 不指定文档 id(随机生成)
curl -XPOST 'localhost:9200/my_index/_doc?pretty' -H 'Content-Type: application/json' -d'
{
"id": 102,
"name": "lisi",
"age": 19
}
'
4.2 查询文档
# 查询所有文档
curl -XGET 'localhost:9200/my_index/_search?pretty'
# 根据 id 查询指定文档
curl -XGET 'localhost:9200/my_index/_doc/1?pretty'
4.3 修改文档
# 全量修改:PUT / POST 均可,全部字段都会被更新
curl -XPOST 'localhost:9200/my_index/_doc/1?pretty' -H 'Content-Type: application/json' -d'
{
"id": 105,
"name": "wangwu",
"age": 20
}
'
# 部分修改:POST + _update,只修改指定字段
curl -XPOST 'localhost:9200/my_index/_doc/1/_update?pretty' -H 'Content-Type: application/json' -d '{"doc": {"age": 40}}'
4.4 删除文档
curl -XDELETE 'localhost:9200/my_index/_doc/1?pretty'
4.5 清空文档
# 清空索引全部文档
curl -XPOST 'localhost:9200/my_index/_delete_by_query?pretty' -H 'Content-Type: application/json' -d '{"query": {"match_all": {}}}'
# 按查询条件清理部分文档
curl -XPOST 'localhost:9200/my_index/_delete_by_query?pretty' -H 'Content-Type: application/json' -d '
{
"query": {
"bool": {
"filter": [
{ "range": { "age": { "gte": 18, "lt": 20 } } }
]
}
}
}
'
五、复杂查询(Complex Query)
语句条件:match 匹配查询、bool 联合查询、term 精确查询 约束条件:must、must_not、should、filter
| match | 先分析文档,通过分词器解析后再查询。text 类型可分词查询;keyword 类型不分词,不支持分词查询 |
| term | 不分词,只做精确查询 |
| bool | 通常搭配 must、should、must_not、filter 使用 |
| must / must_not | 必须满足 / 必须不满足,类似 MySQL 中 AND 的用法 |
| should | 满足其一即可,类似 MySQL 中 OR 的用法 |
| filter | 过滤查询 |
5.1 简单查询(term 精确匹配)
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d '
{
"query": {
"term": { "name": "zhangsan" }
}
}
'
5.2 过滤查询(filter)
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d '
{
"query": {
"bool": {
"filter": [
{ "range": { "age": { "gte": 18, "lt": 20 } } }
]
}
}
}
'
5.3 多条件复合查询(must)
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d '
{
"query": {
"bool": {
"must": [
{ "term": { "name": "zhangsan" } },
{ "term": { "age": 18 } }
]
}
}
}
'
5.4 terms 多值查询(类似 MySQL IN)
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d '
{
"query": {
"bool": {
"must": [
{ "terms": { "age": [18, 19, 20] } }
]
}
}
}
'
5.5 should 嵌套查询
注意:must 和 should 不可以并列使用,must 和 must_not 可以并列使用。
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d '
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{ "term": { "name": "zhangsan" } },
{ "term": { "age": 18 } }
]
}
},
{
"bool": {
"must": [
{ "term": { "id": 105 } },
{ "term": { "age": 18 } }
]
}
}
]
}
}
}
'
六、集群状态异常(Red)处理
当集群状态为 Red 时,说明集群出现异常:有主分片未分配,部分数据不可用,需要及时排查节点状态与分片 / 副本的分配情况。
![[05_中间件/ELK/image/18749913d295840de907ee9fde89687f_MD5.jpg]]
网硕互联帮助中心



评论前必须登录!
注册