Elasticsearch CRUD + 关键字速记手册
基础阶段最重要的 ES 增删改查 + 必背关键字速记版。
假设 Index:
user/product
目录
- 增:Create
- 查:Read
- 改:Update
- 删:Delete
- 复杂查询:bool
- 排序
- 分页
- 只返回指定字段
- [建"表":Index + Mapping](#建"表":Index + Mapping)
- [最重要的 type](#最重要的 type)
- [text + keyword](#text + keyword)
- 必背表
- [ES 入门脑图](#ES 入门脑图)
- [完整 CRUD 小抄(以 product 为例)](#完整 CRUD 小抄(以 product 为例))
1. 增:Create
新增一条文档:
json
POST /user/_doc/1
{
"name": "张三",
"age": 20
}
关键字: POST、_doc
记法:
POST /Index/_doc/ID
2. 查:Read
根据 ID 查
json
GET /user/_doc/1
关键字: GET、_doc
查询全部
json
GET /user/_search
{
"query": {
"match_all": {}
}
}
关键字: _search、query、match_all
| 关键字 | 意思 |
|---|---|
_search |
搜索 |
query |
查询条件 |
match_all |
全部 |
match:全文查询
json
GET /user/_search
{
"query": {
"match": {
"name": "张三"
}
}
}
记法:
query → match → 字段 → 值
term:精确查询
json
GET /user/_search
{
"query": {
"term": {
"status": "PAID"
}
}
}
| 关键字 | 用途 |
|---|---|
match |
全文搜索 |
term |
精确匹配 |
range:范围查询
json
GET /user/_search
{
"query": {
"range": {
"age": {
"gte": 18,
"lte": 30
}
}
}
}
必背:
| 关键字 | 含义 |
|---|---|
gt |
> |
gte |
>= |
lt |
< |
lte |
<= |
3. 改:Update
json
POST /user/_update/1
{
"doc": {
"age": 21
}
}
关键字: _update、doc
记法:
json
POST /Index/_update/ID
{
"doc": {
"字段": "新值"
}
}
doc的意思可以简单理解为:我要修改哪些字段。
4. 删:Delete
删除 Document
json
DELETE /user/_doc/1
关键字: DELETE、_doc
删除整个 Index
json
DELETE /user
⚠️ 这个是删整个 Index,谨慎操作。
5. 复杂查询:bool
ES 查询非常重要的关键字:
json
GET /user/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "张三" } }
],
"filter": [
{ "range": { "age": { "gte": 18 } } }
]
}
}
}
重点记:
bool
├── must
├── filter
├── should
└── must_not
| 关键字 | 含义 |
|---|---|
must |
必须满足 |
filter |
条件过滤 |
should |
尽量匹配 / 或 |
must_not |
不能满足 |
最常见用法:
must→ 搜索条件filter→ 筛选条件
6. 排序
json
GET /user/_search
{
"query": { "match_all": {} },
"sort": [
{ "age": { "order": "desc" } }
]
}
关键字: sort、order、asc、desc
| 关键字 | 含义 |
|---|---|
asc |
升序 |
desc |
降序 |
7. 分页
json
GET /user/_search
{
"from": 0,
"size": 10,
"query": { "match_all": {} }
}
关键字: from、size
| 关键字 | 含义 |
|---|---|
from |
从哪里开始 |
size |
查多少条 |
类似 MySQL:
LIMIT 0, 10
8. 只返回指定字段
json
GET /user/_search
{
"_source": ["name", "age"],
"query": { "match_all": {} }
}
关键字: _source --- 只返回哪些字段。
9. 建"表":Index + Mapping
json
PUT /user
{
"mappings": {
"properties": {
"name": { "type": "text" },
"age": { "type": "integer" },
"status": { "type": "keyword" }
}
}
}
必背: PUT、mappings、properties、type
PUT → 创建 Index
mappings → 定义结构
properties → 定义字段
type → 字段类型
10. 最重要的 type
| 类型 | 用途 |
|---|---|
text |
全文搜索 |
keyword |
精确匹配 / 排序 / 聚合 |
integer / long |
整数 |
double |
小数 |
boolean |
true / false |
date |
日期 |
object |
对象 |
nested |
嵌套对象 |
11. text + keyword
非常重要:
json
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
意思:
name
├── text → 全文搜索
└── name.keyword → 精确匹配 / 排序 / 聚合
所以经常看到 name 和 name.keyword 两种用法。
12. 必背表
| 分类 | 关键字 | 意思 |
|---|---|---|
| 创建 | PUT |
创建 Index |
| 新增 | POST |
新增 Document |
| 查询 | GET |
查询 |
| 修改 | _update |
修改 Document |
| 删除 | DELETE |
删除 |
| 文档 | _doc |
Document API |
| 搜索 | _search |
搜索 |
| 结构 | mappings |
Mapping |
| 字段 | properties |
字段定义 |
| 类型 | type |
数据类型 |
| 全文 | match |
全文搜索 |
| 精确 | term |
精确匹配 |
| 范围 | range |
范围查询 |
| 组合 | bool |
组合查询 |
| 必须 | must |
必须满足 |
| 过滤 | filter |
条件过滤 |
| 或 | should |
尽量匹配 / 或 |
| 排除 | must_not |
排除 |
| 全部 | match_all |
查询全部 |
| 排序 | sort |
排序 |
| 升降 | asc/desc |
升序/降序 |
| 分页 | from/size |
分页 |
| 返回字段 | _source |
指定返回字段 |
13. ES 入门脑图
ES
│
├── Index
│ └── user
│
├── Document
│ └── _doc/1
│
├── Mapping
│ └── properties
│ ├── name → text
│ ├── age → integer
│ └── status → keyword
│
└── Query
│
├── match_all
├── match
├── term
├── range
└── bool
├── must
├── filter
├── should
└── must_not
其他
│
├── sort
├── from
├── size
└── _source
第一阶段背诵顺序:
PUT / POST / GET / DELETE
↓
Index / _doc / _search / _update
↓
mappings / properties / type
↓
query / match / term / range
↓
bool / must / filter / should / must_not
↓
sort / from / size / _source
这套掌握后,再学 aggs(聚合)、nested、analyzer(分词器)、alias、shard/replica,就比较顺了。
14. 完整 CRUD 小抄(以 product 为例)
14.1 创建 Index:先建"表"
json
PUT /product
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"id": { "type": "long" },
"name": {
"type": "text",
"fields": {
"keyword": { "type": "keyword" }
}
},
"category": { "type": "keyword" },
"price": { "type": "double" },
"stock": { "type": "integer" },
"status": { "type": "keyword" },
"description": { "type": "text" },
"create_time": { "type": "date" }
}
}
}
重要关键字: PUT、settings、number_of_shards、number_of_replicas、mappings、properties、type
14.2 新增:Create
新增一条:
json
POST /product/_doc/1
{
"id": 1,
"name": "苹果 iPhone 17 Pro",
"category": "手机",
"price": 6999,
"stock": 100,
"status": "ON_SALE",
"description": "苹果最新款手机",
"create_time": "2026-09-17T10:00:00"
}
批量新增 _bulk(实际项目里非常重要):
json
POST /_bulk
{"index":{"_index":"product","_id":"2"}}
{"id":2,"name":"华为 Mate 80","category":"手机","price":5999,"stock":50,"status":"ON_SALE"}
{"index":{"_index":"product","_id":"3"}}
{"id":3,"name":"小米 16","category":"手机","price":3999,"stock":200,"status":"ON_SALE"}
14.3 查询:Read --- 一个"大查询"模板
需求: 搜索商品名称"手机",价格 3000~8000,库存大于 0,只查询上架商品,分类是手机;支持分页;按照价格从低到高;同时返回商品总数。
json
GET /product/_search
{
"from": 0,
"size": 20,
"_source": ["id", "name", "category", "price", "stock", "status"],
"query": {
"bool": {
"must": [
{ "match": { "name": "手机" } }
],
"filter": [
{ "term": { "category": "手机" } },
{ "term": { "status": "ON_SALE" } },
{ "range": { "price": { "gte": 3000, "lte": 8000 } } },
{ "range": { "stock": { "gt": 0 } } }
],
"must_not": [
{ "term": { "status": "DELETED" } }
],
"should": [
{ "match": { "description": "手机" } }
]
}
},
"sort": [
{ "price": { "order": "asc" } }
]
}
查询结构拆解:
_search
│
├── from
├── size
├── _source
│
├── query
│ └── bool
│ ├── must → match
│ ├── filter → term, term, range, range
│ ├── must_not → term
│ └── should → match
│
└── sort
14.4 更新:Update
更新部分字段:
json
POST /product/_update/1
{
"doc": {
"price": 6499,
"stock": 80,
"status": "ON_SALE"
}
}
根据条件批量更新 _update_by_query:
json
POST /product/_update_by_query
{
"query": {
"term": { "category": "手机" }
},
"script": {
"source": "ctx._source.status = 'OFF_SALE'"
}
}
重要关键字: _update_by_query、script
14.5 删除:Delete
根据 ID 删除:
json
DELETE /product/_doc/1
根据条件批量删除 _delete_by_query:
json
POST /product/_delete_by_query
{
"query": {
"term": { "status": "DELETED" }
}
}
14.6 CRUD 小抄总览
========================
创建 Index
========================
PUT /product
mappings / properties / type
========================
新增
========================
POST /product/_doc/1
{ ... }
========================
查询
========================
GET /product/_doc/1
GET /product/_search
query → bool → must / filter / should / must_not
match / term / range
from / size / _source / sort
========================
修改
========================
POST /product/_update/1 → doc
POST /product/_update_by_query → query + script
========================
删除
========================
DELETE /product/_doc/1
POST /product/_delete_by_query → query
========================
批量操作
========================
POST /_bulk
优先背诵清单
PUT / POST / GET / DELETE
_index / _doc / _search / _update / _bulk
mappings / properties / type
query / bool / must / filter / should / must_not
match / term / range
gt / gte / lt / lte
sort / asc / desc
from / size / _source
_update_by_query / _delete_by_query / script
最核心的查询关系(形成条件反射):
query
↓
bool
↓
┌──────────┬──────────┬──────────┬──────────┐
must filter should must_not
↓ ↓ ↓ ↓
match term match term
range
这套掌握以后,再去学 ES 的聚合
aggs、nested、分词analyzer、高亮highlight、collapse,会容易很多。