文档-删除
逻辑删除 删除一个文档不会立即从磁盘上移除,它只是被标记成已删除
# 按主键删除 DELETE _doc
在 Postman 中,向 ES 服务器发 DELETE 请求 : http://localhost:9200/shopping/_doc/1
DELETE http://localhost:9200/shopping/_doc/1
1
返回信息
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 8, // 对数据的操作,都会更新版本
"result": "deleted", // 表示删除成功
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 10,
"_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
再次查询
GET http://localhost:9200/shopping/_doc/1
返回
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"found": false
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
删除一个不存在的文档时
DELETE http://localhost:9200/shopping/_doc/3
1
返回
{
"_index": "shopping",
"_type": "_doc",
"_id": "3",
"_version": 1,
"result": "not_found", // 表示没有找到
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 13,
"_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 按条件删除 POST _delete_by_query
一般删除数据都是根据文档的唯一性标识进行删除,实际操作时,也可以根据条件对多条数据进行删除
先分别增加多条记录
POST http://localhost:9200/shopping/_doc
1
请求体分别为
{
"title":"小米手机",
"category":"小米",
"images":"http://my-notes.cn/xm.jpg",
"price":4000.00
}
1
2
3
4
5
6
2
3
4
5
6
{
"title":"华为手机",
"category":"华为",
"images":"http://my-notes.cn/hw.jpg",
"price":4000.00
}
1
2
3
4
5
6
2
3
4
5
6
向 ES 服务器发 POST
请求进行删除操作,注意使用关键字_delete_by_query
删除价格为4000的记录
POST http://localhost:9200/shopping/_delete_by_query
1
请求体JSON
{
"query":{
"match":{
"price":4000.00
}
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
返回JSON
{
"took": 837, // 耗时
"timed_out": false, // 是否超时
"total": 2, // 总数
"deleted": 2, // 删除数量
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1.0,
"throttled_until_millis": 0,
"failures": []
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Last Updated: 2022/02/05, 15:58:51