ES的基础操作8.*

 一、三个核心概念(必须先懂)

概念类比
index 数据库
document 一条记录
field 字段

例子:

 
{
"name": "tom",
"age": 25
}
 

🚀 二、基础操作(CRUD)

下面所有操作都可以在 curl 或 Kibana Dev Tools 执行


1️⃣ 创建索引(数据库)

 
PUT /users
 

2️⃣ 插入数据(Create)

 
POST /users/_doc/1
{
"name": "tom",
"age": 25,
"job": "java"
}
 

👉 _doc/1 = 数据ID


3️⃣ 查询单条(Read)

 
GET /users/_doc/1
 

4️⃣ 查询全部(Search)

 
GET /users/_search
{
"query": {
"match_all": {}
}
}
 

🔍 5️⃣ 条件搜索(核心能力)

模糊搜索(类似搜索引擎)

 
GET /users/_search
{
"query": {
"match": {
"job": "java"
}
}
}
 

精确匹配(不分词)

 
GET /users/_search
{
"query": {
"term": {
"age": 25
}
}
}
 

多条件查询(非常常用)

 
GET /users/_search
{
"query": {
"bool": {
"must": [
{ "match": { "job": "java" }},
{ "term": { "age": 25 }}
]
}
}
}
 

✏️ 6️⃣ 修改数据(Update)

 
POST /users/_update/1
{
"doc": {
"age": 30
}
}
 

❌ 7️⃣ 删除数据

删除单条:

 
DELETE /users/_doc/1
 

删除索引:

 
DELETE /users
 

⚠️ 小心:会删库


📊 三、常用查询技巧

1️⃣ 查询所有索引

 
GET _cat/indices?v
 

2️⃣ 查看索引结构

 
GET /users
 

3️⃣ 看数据量

 
GET /users/_count
 

🧠 四、ES 查询核心逻辑

你要记住:

🔥 match

👉 分词搜索(适合搜索框)

🔥 term

👉 精确匹配(适合ID / 数字)

🔥 bool

👉 多条件组合


🚀 五、最重要的一张图(记住就够了)

 
ES = index(库)
└── document(行)
└── field(列)
 

🔥 六、你现在可以做什么练习

直接执行这个小练习👇

① 建索引

 
PUT /blog
 

② 插入数据

 
POST /blog/_doc/1
{
"title": "hello es",
"content": "this is my first document",
"tags": "java"
}
 

③ 搜索

 
GET /blog/_search
{
"query": {
"match": {
"content": "first"
}
}
}
posted @ 2026-04-27 13:39  albert飞的博客  阅读(20)  评论(0)    收藏  举报