索引的批量操作

一、基于_mget的批量查询

1、批量查询指定id

1 GET product/_mget
2 {
3   "ids":[2,3]
4 }

2、查询指定id中某些字段

 1 GET product/_mget
 2 {
 3   "docs": [
 4     {
 5       "_id": 2,
 6       "_source": [
 7         "name",
 8         "price"
 9       ]
10     },
11     {
12       "_id": 3,
13       "_source": {
14         "include": [
15           "name",
16           "price"
17         ],
18         "exclude": [
19           "lv",
20           "type"
21         ]
22       }
23     }
24   ]
25 }

二、文档的四种操作类型

1、create:不存在则创建,存在则报错

1 PUT test/_doc/1
2 {
3   "name":1
4 }

创建索引时,自动创建id

1 POST test1/_doc
2 {
3   "name":"lyc"
4 }

2、delete:删除文档,es中删除是一种懒删除(被标记为删除),并不会被物理删除。es中会有version进行记录版本,ctrl+enter操作后,结果中会出现version

3、update:全量替换或部分更新

 1 #全量替换
 2 PUT test/_doc/1
 3 {
 4   "name":"lyc",
 5   "age":25
 6 }
 7 #部分替换
 8 POST test/_update/1
 9 {
10   "doc": {
11     "name":"lycc"
12   }
13 }

4、index:索引(动词)

创建:PUT test/_create/1

全量替换:PUT test/_doc/1

三、索引的批量操作_bulk

 1 #加?filter_path=items.*.error  只显示失败的
 2 POST /_bulk?filter_path=items.*.error
 3 { "delete": { "_index": "product2",  "_id": "1" }}
 4 { "create": { "_index": "product2",  "_id": "2" }}
 5 { "name":    "_bulk create 2" }
 6 { "create": { "_index": "product2",  "_id": "12" }}
 7 { "name":    "_bulk create 12" }
 8 { "index":  { "_index": "product2",  "_id": "3" }}
 9 { "name":    "index product2 " }
10 { "index":  { "_index": "product2",  "_id": "13" }}
11 { "name":    "index product2" }
12 { "update": { "_index": "product2",  "_id": "4","retry_on_conflict" : "3"} }
13 { "doc" : {"test_field2" : "bulk test1"} }

优点:不会消耗额外的堆内存空间,如果使用之前put之类的操作会消耗额外的内存空间,要序列化成json

缺点:对于人类而言可读性较差

 

posted @ 2022-02-13 22:42  showMeTheCodes  阅读(152)  评论(0编辑  收藏  举报