【无标题】

Elasticsearch CRUD + 关键字速记手册

基础阶段最重要的 ES 增删改查 + 必背关键字速记版。

假设 Index:user / product


目录

  1. 增:Create
  2. 查:Read
  3. 改:Update
  4. 删:Delete
  5. 复杂查询:bool
  6. 排序
  7. 分页
  8. 只返回指定字段
  9. [建"表":Index + Mapping](#建"表":Index + Mapping)
  10. [最重要的 type](#最重要的 type)
  11. [text + keyword](#text + keyword)
  12. 必背表
  13. [ES 入门脑图](#ES 入门脑图)
  14. [完整 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,会容易很多。

相关推荐
小π军30 分钟前
操作系统面试题
java·linux·服务器
倔强的石头1061 小时前
应用账号最小权限实践:读写账号、报表账号、运维账号分层
java·运维·开发语言
Shaoshing1 小时前
ThreadLocal
java·jvm·threadlocal
写后端的胖头鱼1 小时前
一文详解CAS
java·jvm·spring·cas·乐观锁·自旋
鱼宵2 小时前
Spring AI 提示词模板:{变量} 参数化 + few-shot,一条提示词反复用
java·人工智能·spring·few-shot·提示词工程·springai
蜗牛互联网2 小时前
Python消费Responses SSE事件:增量文本、超时与取消
java·开发语言·人工智能·后端·python
wanderist.2 小时前
线性筛法详解:从筛质数到欧拉函数、Möbius 函数与约数函数
java·数据结构·算法
All for pursuit.2 小时前
【设计-1】208.实现Trie (前缀树)
数据结构·c++·算法·leetcode
weixin_307779132 小时前
C++代码实现MATLAB中的crossval函数功能
开发语言·c++·算法·matlab
青少儿编程课堂2 小时前
背包问题(0/1 背包与完全背包)解题精讲——动态规划入门
c++·python·算法·bfs·信息学竞赛