六、Elasticsearch 實戰

基於groovy腳本進行partial update

造數據

PUT /test_index/test_type/11
{
  "num":0,
  "tags":[]
}

內部腳本

POST /test_index/test_type/11/_update
{
  "script": "ctx._source.num+=1"
}

外部腳本

  • 5.x 支持外部腳本,目錄:elasticseach/config/scripts
  • 7.x 不支持外部腳本

mget批量查詢

查詢 indextype 都不同的數據

GET _mget
{
  "docs": [
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": 1
    },
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": 2
    }
  ]
}

查詢同一個index不同type數據

GET /test_index/_mget
{
  "docs": [
    {
      "_type": "test_type",
      "_id": 1
    },
    {
      "_type": "test_type",
      "_id": 2
    }
  ]
}

查詢indextype都相同的數據

GET /test_index/test_type/_mget
{
    "ids": [1, 4]
}

bulk 批量增刪改

功能

  • create : PUT /index/type/id/_create 強制創建
  • index : 普通的 PUT 操作,可以創建或者全量替換文檔
  • delete : 刪除 document
  • update : 執行 partial update 操作
  • 注意:a).bulk api 對 json 的語法有嚴格的要求,每個json串內部不能換行, 每個json 串之間必須換行; b).bulk 操作中任何一個操作失敗了,是不會影響其他的操作的,但是在返回結果中會以異常日誌的方式體現; c).bulk request 會加載到內存裏,如果請求太大的的話,性能反而會下降,因此需要反覆嘗試出一個最佳的bulk size。一般從 1000 - 5000 條數據開始,嘗試逐步增加。另一方面,如果看大小的話,最好是在5~15MB之間
POST /_bulk
{"delete":{"_index":"test_index","_type":"test_type","_id":11}}
{"create":{"_index":"test_index","_type":"test_type","_id":3}}
{"test_field":"test 3"}
{"index":{"_index":"test_index","_type":"test_type","_id":4}}
{"test_field":"replaced test 4"}
{"update":{"_index":"test_index","_type":"test_type","_id":1}}
{"doc":{"test_field":"partial update test 1"}}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章