【Elasticsearch基础】CRUD操作实践

devtools/2025/4/2 12:06:02/

Elasticsearch作为最流行的搜索和分析引擎,其核心CRUD(创建、读取、更新、删除)操作是每个开发者必须掌握的技能。本文将详细介绍Elasticsearch的基础数据操作,并提供可直接复用的curl示例。

1 创建索引与文档

1.1 创建索引

// 语法
PUT /<index_name>
{"settings": { ... },  # 索引配置"mappings": { ... }   # 字段映射
}
// 创建简单索引(自动应用默认映射)
curl -X PUT "http://192.168.10.33:9200/myindex" -H 'Content-Type: application/json' -d'
{"settings": {"number_of_shards": 3,"number_of_replicas": 1}
}'// 创建带自定义映射的索引
curl -X PUT "http://192.168.10.33:9200/myindex_1" -H 'Content-Type: application/json' -d'
{"mappings": {"properties": {"title": { "type": "text" },"views": { "type": "integer" },"publish_date": { "type": "date" }}}
}'

1.2 添加文档

# 指定ID创建文档
curl -X POST "http://192.168.10.33:9200/myindx/_doc/1" -H 'Content-Type: application/json' -d'
{"name": "无线耳机","price": 799,"stock": 200
}'# 自动生成ID创建文档
curl -X POST "http://192.168.10.33:9200/myindex_1/_doc" -H 'Content-Type: application/json' -d'
{"name": "智能手表","price": 2999
}'# 批量创建文档(更高效)
curl -X POST "http://192.168.10.33:9200/_bulk" -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "myindex", "myindex_1" : "2" } }
{ "name": "蓝牙音箱", "price": 299 }
{ "index" : { "_index" : "myindex", "_id" : "3" } }
{ "name": "电子书阅读器", "price": 899 }
'

2 查询文档

2.1 基础查询

# 按ID查询文档
curl -X GET "http://192.168.10.33:9200/myindex_1/_doc/1?pretty"# 检查文档是否存在
curl -I "http://192.168.10.33:9200/myindex/_doc/1"# 获取多个文档
curl -X GET "http://192.168.10.33:9200/_mget" -H 'Content-Type: application/json' -d'
{"docs": [{ "_index": "myindex_1", "_id": "1" },{ "_index": "myindex_1", "_id": "2" }]
}'

2.2 搜索API

# 简单全文搜索
curl -X GET "http://192.168.10.33:9200/myindex/_search" -H 'Content-Type: application/json' -d'
{"query": {"match": {"name": "智能"}}
}'# 复合查询
curl -X GET "http://192.168.10.33:9200/myindx/_search" -H 'Content-Type: application/json' -d'
{"query": {"bool": {"must": [{ "match": { "name": "电子" } }],"filter": [{ "range": { "price": { "gte": 500, "lte": 1000 } } }]}},"sort": [{ "price": { "order": "desc" } }],"from": 0,"size": 5
}'

3 更新文档

3.1 全量替换

# 完全替换文档内容(需提供所有字段)
curl -X PUT "http://192.168.10.33:9200/myindx/_doc/1" -H 'Content-Type: application/json' -d'
{"name": "无线耳机","price": 699,"color": "black"
}'

3.2 部分更新

# 更新单个字段
curl -X POST "http://192.168.10.33:9200/myindex/_update/1" -H 'Content-Type: application/json' -d'
{"doc": {"price": 649}
}'# 带条件的更新(仅当stock>0时执行)
curl -X POST "http://192.168.10.33:9200/myindex/_update/1" -H 'Content-Type: application/json' -d'
{"script": {"source": "ctx._source.price -= params.discount","params": {"discount": 50}},"upsert": {"name": "无线耳机","price": 699}
}'# 批量更新
curl -X POST "http://192.168.10.33:9200/myindex_1" -H 'Content-Type: application/json' -d'
{ "update" : { "_index" : "myindex", "_id" : "1" } }
{ "doc" : { "price" : 599 } }
{ "update" : { "_index" : "myindex", "_id" : "2" } }
{ "script" : { "source": "ctx._source.stock -= 1" } }
'

4 删除操作

4.1 删除文档

# 按ID删除
curl -X DELETE "http://192.168.10.33:9200/myindx/_doc/1"# 按查询删除
curl -X POST "http://192.168.10.33:9200/myindx/_delete_by_query" -H 'Content-Type: application/json' -d'
{"query": {"range": {"price": { "lt": 300 }}}
}'# 批量删除
curl -X POST "http://192.168.10.33:9200/myindx_1" -H 'Content-Type: application/json' -d'
{ "delete" : { "_index" : "myindx", "_id" : "2" } }
{ "delete" : { "_index" : "myindx", "_id" : "3" } }
'

4.2 删除索引

# 删除整个索引(不可逆操作!)
curl -X DELETE "http://192.168.10.33:9200/myindx"# 删除多个索引
curl -X DELETE "http://192.168.10.33:9200/index1,index2"# 使用通配符删除
curl -X DELETE "http://192.168.10.33:9200/myindx_2025_01_*"

5 常见错误与解决方案

错误现象

可能原因

解决方案

无法创建文档(409 Conflict)

文档ID已存在

使用PUT覆盖或生成新ID

更新文档版本冲突(409 Conflict)

文档被其他进程修改

获取最新版本后重试

查询返回部分结果(503 Service Unavailable)

分片未分配

检查集群状态,调整分片分配策略

索引只读(403 Forbidden)

磁盘空间不足

清理空间或调整索引配置


http://www.ppmy.cn/devtools/172072.html

相关文章

程序员软件工具推荐列表

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 程序员软件工具推荐列表1. Snipaste2. VSCod…

Python入门基础

python基础类型转换 str()与int()类型转换 name 张三 age 20 print(type(name),type(age))print(我叫name 今年&#xff0c; str(age)岁 )a10 b198.8 cFalse print(type(a),type(b),type(c)) print(str(a),str(b),str(c))s1 128 f198.7 s276.77 ffTrue s3hello print(type(s…

什么是索引下推和索引覆盖?

一、索引下推&#xff08;Index Condition Pushdown, ICP&#xff09; 1. 什么是索引下推&#xff1f; 核心思想&#xff1a;在存储引擎层&#xff08;如 InnoDB&#xff09;提前过滤数据&#xff0c;减少不必要的回表操作。通俗解释&#xff1a;假设你有一个联合索引&#x…

71. 我的第一个Linux驱动实验

一、字符设备驱动框架 字符设备驱动的编写主要就是驱动对应的open、close、read。。。其实就是 file_operations结构体的成员变量的实现。 其中关于 C 库以及如何通过系统调用“陷入” 到内核空间这个我们不用去管&#xff0c;我们重点关注的是应用程序和具体的驱动&#xff0…

求最大公约数与最小公倍数

求最大公约数&#xff08;GCD&#xff09;与最小公倍数&#xff08;LCM&#xff09; 1. 基本概念 GCD&#xff08;最大公约数&#xff09;&#xff1a;两个整数的最大公共因数LCM&#xff08;最小公倍数&#xff09;&#xff1a;两个整数的最小公共倍数数学关系&#xff1a;L…

微软下一个大更新:Windows 11 25H2或已在路上!

快科技3月26日消息&#xff0c;随着Windows 10支持即将在10月结束&#xff0c;越来越多的用户开始转向Windows 11 24H2&#xff0c;这也是目前强制性更新的最新版本。 不过Windows 11 24H2问题却一点不少&#xff0c;如蓝牙音频故障、文件资源管理器错误&#xff0c;甚至系统崩…

线程同步——读写锁

Linux——线程同步 读写锁 目录 一、基本概念 1.1 读写锁的基本概念 1.2 读写锁的优点 1.3 读写锁的实现 1.4 代码实现 一、基本概念 线程同步中的读写锁&#xff08;Read-Write Lock&#xff09;&#xff0c;也常被称为共享-独占锁&#xff08;Shared-Exclusive Lock&a…

每日一题第15届蓝桥杯c/c++本科B组省赛第3题

#include<iostream> using namespace std; int jud(int a) {int c 1;//位数while (a) {int t a % 10;if (c % 2 ! 0) {//奇数位if (t % 2 0)return 0;//偶数不符合}else {//偶数位if (t % 2 ! 0)return 0;//奇数不符合}c;a / 10;}return 1; } int main() {int count …