创建文档
PUT /<target>/_doc/<_id>
POST /<target>/_doc/
PUT /<target>/_create/<_id>
POST /<target>/_create/<_id>
删除文档
// 根据 id 删除
DELETE /<index>/_doc/<_id>
// 根据查询删除
POST /<target>/_delete_by_query
删除 id 为1的数据
DELETE user/_doc/1
删除全部数据
POST user/_delete_by_query
{
"query": {
"match_all": {}
}
}
更新文档
// 根据 id 更新
POST /<index>/_update/<_id>
// 根据查询更新
POST /<target>/_update_by_query
将 id 为1的数据的 name 修改为张三(修改后)
POST user/_update/1
{
"doc": {
"name": "张三(修改后)"
}
}
将 id 为1,2的数据 age 修改为70
POST user/_update_by_query
{
"query": {
"ids": {
"values": [1, 2]
}
},
"script": {
"source": "ctx._source.age = 70"
}
}
迁移文档
POST _reindex
{
"source": {
"index": "my-index-000001"
},
"dest": {
"index": "my-new-index-000001"
}
}
并发控制
批量操作
批量操作对json有严格的要求,每个json串不能换行,只能放在同一行,相邻的json串之间必须要有换行。每个操作必须是一对json串(delete语法除外)
{ action: { metadata }}
{ request body }
{ action: { metadata }}
{ request body }
操作类型 | 介绍 |
---|---|
create | 文档id不存在则创建,不存在则报错 |
index | 文档id不存在则创建,存在则更新文档 |
update | 根据文档id更新文档,不存在则返回错误 |
delete | 根据文档id删除文档,不存在则返回错误 |
批量新增
{"index": {"_id": 1}}
{"name": "张三", "age": 10, "email": "1.qq.com", "address": "北京朝阳"}
{"index": {"_id": 2}}
{"name": "李四", "age": 20, "email": "2.qq.com", "address": "北京西城"}
{"index": {"_id": 3}}
{"name": "王五", "age": 30, "email": "3.qq.com", "address": "北京东城"}
{"index": {"_id": 4}}
{"name": "赵六", "age": 40, "email": "4.qq.com", "address": "北京海淀"}
文档写入
文档查询
参考博客
官方文档
[0]https://www.elastic.co/guide/en/elasticsearch/reference/8.11/docs.html
[1]https://blog.csdn.net/weixin_39723544/article/details/109237175
[2]https://cloud.tencent.com/developer/article/2133017
大佬博客
[3]https://elasticstack.blog.csdn.net/article/details/128835177