iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 12
0
Elastic Stack on Cloud

親愛的,我把ElasticSearch上雲了系列 第 12

Day 12 Bulk upload document

Day 12 Bulk upload document


前言

昨天講解完了如何添加Documents到index中,以及document層級的一些操作。今天我們繼續專注在Documnets層次的操作,包含批次上傳以及批次取得。


批次取得-_mget

GET /_mget
GET /<index>/_mget

首先介紹一下_mget,_mget意思就是mutiple get,可以在一次query中,取得多個document的回傳,而如何具體設定要取得document的條件如下


{
  "docs": [
    {
      "_index": "demo-index",
      "_id": "1"
    },
    {
      "_index": "demo-index",
      "_id": "2"
    }
  ]
}

我們透過昨天的sample,查詢demo-index中id 1及2的,而demo-index中指有id1的,因此回傳如下,第一個有取得到,第二個則會告訴我們"found": false,代表沒有找到這個文件

response:
{
    "docs": [
        {
            "_index": "demo-index",
            "_type": "_doc",
            "_id": "1",
            "_version": 8,
            "_seq_no": 10,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name": "sron",
                "age": 29,
                "job": "student"
            }
        },
        {
            "_index": "demo-index",
            "_type": "_doc",
            "_id": "2",
            "found": false
        }
    ]
}

可以看到,查詢中的json,裡面有index欄位,因此它可以同時取得不同index的document,而若同一個index,取得不同id,則可以透過下面這種方式簡化。

GET /demo-index/_mget
{
  "ids" : ["1", "2"]
}

回傳就會跟上面完全一樣。

而針對document層次的get,我們也可以限定每個_document我們自訂資料的_source條件限制,如下

GET /_mget
{
  "docs": [
    {
      "_index": "demo-index",
      "_type": "_doc",
      "_id": "1",
      "_source": false
    },
    {
      "_index": "demo-index",
      "_type": "_doc",
      "_id": "2",
      "_source": [ "field3", "field4" ]
    },
    {
      "_index": "demo-index",
      "_type": "_doc",
      "_id": "3",
      "_source": {
        "include": [ "user" ],
        "exclude": [ "user.location" ]
      }
    }
  ]
}

可以限定_source中要包含或不要哪些field。

bulk

bulk其實就是可以讓我們快速處理document的一個手段,透過對於bulk API的endpoint去request,就可以一次批量處理document。

POST _bulk
{ "index" : { "_index" : "demo-index", "_id" : "2" } }
{ "name" : "Bob" }
{ "delete" : { "_index" : "demo-index", "_id" : "3" } }
{ "create" : { "_index" : "demo-index", "_id" : "4" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "demo-index"} }
{ "doc" : {"field2" : "value2"} }

首先先跟大家講解一下,bulk API其實可以想像成他是透過多個指令,存在同一個request中去觸發,因此你在發送request的時候,會發現BODY(如上)會是多個json的組合,因此Content-type記得要從application/json,換成application/x-ndjson,另外最後結尾必須要有空列。

每個指令之間透過\n去區隔。

其中bulk常用的指令有:index、delete、crete、update,index可以想像就是之前我們所提到的_doc,也就是不管id有沒有重複,都會蓋過去;create則就是_create,delete也就是一樣的意思。

透過這個bulk的操作,可以一次將一堆的document去進行create、delete或是update,當然你也可以直接透過curl去操作

$ cat requests
{ "index" : { "_index" : "demo-index", "_id" : "1" } }
{ "field1" : "value1" }
$ curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary "@requests"; echo
{"took":7, "errors": false, "items":[{"index":{"_index":"demo-index","_type":"_doc","_id":"1","_version":1,"result":"created","forced_refresh":false}}]}

上一篇
Day 11 為建構好的index增加document
下一篇
Day 13 DataField And Mapping
系列文
親愛的,我把ElasticSearch上雲了30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言