【无标题】

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": {}
  }
}

关键字: _searchquerymatch_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
  }
}

关键字: _updatedoc

记法:

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" } }
  ]
}

关键字: sortorderascdesc

关键字 含义
asc 升序
desc 降序

7. 分页

json 复制代码
GET /user/_search
{
  "from": 0,
  "size": 10,
  "query": { "match_all": {} }
}

关键字: fromsize

关键字 含义
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" }
    }
  }
}

必背: PUTmappingspropertiestype

复制代码
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   → 精确匹配 / 排序 / 聚合

所以经常看到 namename.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" }
    }
  }
}

重要关键字: PUTsettingsnumber_of_shardsnumber_of_replicasmappingspropertiestype

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_queryscript

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 的聚合 aggsnested、分词 analyzer、高亮 highlightcollapse,会容易很多。

相关推荐
用户3126874877201 小时前
Java SPI 到底怎么实现动态扩展的?从 ServiceLoader 到 Dubbo SPI 全链路拆解
java
新时代农民工~1 小时前
【双机高可用部署方案-前后端部署】
java·nginx·springboot
企业数字化笔记1 小时前
固定资产还有借用记录能报废吗?Java前置检查、停止折旧与SQL验收
java·开发语言·sql
飞虹地星海1 小时前
从零到一跑通苍穹外卖:一个大二学生的暑假项目复盘
java
她的男孩1 小时前
账号锁定配了"错 4 次锁 30 分钟",我连错 100 次一次没锁上:扒完 4091 行认证源码,找到 5 个坑
java·后端·架构
程序猿乐锅1 小时前
【黑马点评 | 第二篇】Redis 缓存更新策略与商铺缓存实现
java·网络·redis·spring·mybatis
livemetee1 小时前
MySQL联合唯一索引(code,deleted)与逻辑删除问题
java·数据库
MacroZheng1 小时前
Redis官方发布高颜值可视化工具,功能更是强的离谱!
java·redis·后端
wuminyu1 小时前
纯轻量级锁体系下C2编译器锁粗化和消除机制剖析
java·linux·c语言·jvm·c++