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) |
磁盘空间不足 |
清理空间或调整索引配置 |