一、文檔批量操作
這裡多個文檔是指,批量操作多個文檔,搜索查詢文檔將在之後的章節講解
1.批量獲取文檔數據
批量獲取文檔數據是通過_mget的API來實現的
(1)在URL中不指定index和type
- 請求方式:GET
- 請求地址:_mget
- 功能說明 : 可以通過ID批量獲取不同index和type的數據
請求參數:
- docs : 文檔數組參數
- _index : 指定index
- _type : 指定type
- _id : 指定id
- _source : 指定要查詢的字段
| 1 GET _mget2 {3 “docs”: [4 {5 “_index”: “es_db”,6 “_type”: “_doc”,7 “_id”: 18 },9 {10 “_index”: “es_db”,11 “_type”: “_doc”,12 “_id”: 213 }14 ]15 } |
響應結果如下:
| 1 {2 “docs” : [3 {4 “_index” : “es_db”,5 “_type” : “_doc”,6 “_id” : “1”,7 “_version” : 3,8 “_seq_no” : 7,9 “_primary_term” : 1,10 “found” : true,11 “_source” : {12 “name” : “張三666”,13 “sex” : 1,14 “age” : 25,15 “address” : “廣州天河公園”,16 “remark” : “java developer”17 }18 },19 {20 “_index” : “es_db”,21 “_type” : “_doc”,22 “_id” : “2”,23 “_version” : 1,24 “_seq_no” : 1,25 “_primary_term” : 1,26 “found” : true,27 “_source” : {28 “name” : “李四”,29 “sex” : 1,30 “age” : 28,31 “address” : “廣州荔灣大廈”,32 “remark” : “java assistant”33 }34 }35 ]36 } |
(2)在URL中指定index
- 請求方式:GET
- 請求地址:/{{indexName}}/_mget
- 功能說明 : 可以通過ID批量獲取不同index和type的數據請求參數:
docs : 文檔數組參數
- _index : 指定index
- _type : 指定type
- _id : 指定id
- _source : 指定要查詢的字段
| 1 GET /user/_mget2 {3 “docs”: [4 {5 “_type”:”_doc”,6 “_id”: 37 },8 {9 “_type”:”_doc”,10 “_id”: 411 }12 ]13 } |
(3)在URL中指定index和type
- 請求方式:GET
- 請求地址:/{{indexName}}/{{typeName}}/_mget
- 功能說明 : 可以通過ID批量獲取不同index和type的數據
請求參數:
- docs : 文檔數組參數
- _index : 指定index
- _type : 指定type
- _id : 指定id
- _source : 指定要查詢的字段
| 1 GET /es_db/_doc/_mget2 {3 “docs”: [4 {5 “_id”: 16 },7 {8 “_id”: 29 }10 ]11 } |
2.批量操作文檔數據
批量對文檔進行寫操作是通過_bulk的API來實現的
- 請求方式:POST
- 請求地址:_bulk
- 請求參數:通過_bulk操作文檔,一般至少有兩行參數(或偶數行參數)
- 第一行參數為指定操作的類型及操作的對象
(index,type和id)
- 第二行參數才是操作的數據
參數類似於:
| 1 {“actionName”:{“_index”:”indexName”, “_type”:”typeName”,”_id”:”id”}}2 {“field1″:”value1”, “field2″:”value2”} |
- actionName:表示操作類型,主要有create,index,delete和update
(1)批量創建文檔create
| 1 POST _bulk2 {“create”:{“_index”:”article”, “_type”:”_doc”, “_id”:3}}3 {“id”:3,”title”:”老師1″,”content”:”老師666″,”tags”:[“java”, “面向對象”],”create_time”:155402530}4 {“create”:{“_index”:”article”, “_type”:”_doc”, “_id”:4}}5 {“id”:4,”title”:”老師2″,”content”:”老師NB”,”tags”:[“java”, “面向對象”],”create_time”:15542530} |
(2)普通創建或全量替換index
| 1 POST _bulk2 {“index”:{“_index”:”article”, “_type”:”_doc”, “_id”:3}}3 {“id”:3,”title”:”老師(一)”,”content”:”老師666″,”tags”:[“java”, “面向對象”],”create_time”:1552530}4 {“index”:{“_index”:”article”, “_type”:”_doc”, “_id”:4}}5 {“id”:4,”title”:”老師(二)”,”content”:”老師NB”,”tags”:[“java”, “面向對象”],”create_time”:1552530} |
- 如果原文檔不存在,則是創建
- 如果原文檔存在,則是替換(全量修改原文檔)
(3)批量刪除delete
| 1 POST _bulk2 {“delete”:{“_index”:”article”, “_type”:”_doc”, “_id”:3}}3 {“delete”:{“_index”:”article”, “_type”:”_doc”, “_id”:4}} |
(4)批量修改update
| 1 POST _bulk2 {“update”:{“_index”:”article”, “_type”:”_doc”, “_id”:3}}3 {“doc”:{“title”:”ES大法必修內功”}}4 {“update”:{“_index”:”article”, “_type”:”_doc”, “_id”:4}}5 {“doc”:{“create_time”:15508}} |
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/216888.html
微信掃一掃
支付寶掃一掃