1. 程式人生 > >ElasticSearch學習筆記1-修改文件

ElasticSearch學習筆記1-修改文件

本文中所有用[]括起來的和xxx都是變數

基本操作:
1、根據id修改文件,使用POST請求

POST /[index]/[type]/[id]/_update
{
  "doc": {"[XXX]":"[xxxxx]"}
}

舉例:

image
上面這個圖片中index為test,type為doc,id為1,我們將裡面的name改為“name03” age改為20,請求如下:

POST  /test/doc/_update
{
    "doc": {
        "name": "name03",
        "age": 20
    }
}

返回結果為:

{
    "_index": "test",
    "_type": "doc",
    "_id": "1",
    "_version": 13,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 13,
    "_primary_term": 1
}

對該id再進行一次查詢,結果如圖所示:
image

2、使用指令碼修改

POST /[index]/[type]/[id]/_update
{
  "script" : "ctx._source.[xxx] = [xxx]"
}

繼續使用上面的文件

POST  /test/doc/1/_update
{
   "script":"ctx._source.age+=5;ctx._source.name=\"name04\""
}

查詢結果:
image
修改成功。
3、刪除

DELETE   /[index]/[type]/[id]

比較簡單,不做舉例
4、批量建立
批量建立和批量修改的請求都是一樣的, 傳的引數不一樣
請求:

POST    /[index]/[type]/_bulk

批量建立引數:
{"index":{"_id":"xxx"}}
{"xxx": "xxx" }
{"index":{"_id":"xxx"}}
{"xxx": "xxx" }

舉例:

POST /test01/doc01/_bulk
{"index":{"_id":"1"}}
{"name": "name01" }
{"index":{"_id":"2"}}
{"name": "name02","age":20 }
{"index":{"_id":"3"}}
{"name": "name03","age":20 }   --------------1

這裡有一點要注意:在圖中1的位置,後面一定要換行,否則會報錯,提示需要新的一行作為結束的標識,報錯資訊如下

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "The bulk request must be terminated by a newline [\n]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "The bulk request must be terminated by a newline [\n]"
    },
    "status": 400
}

image
image
注意上面兩張圖片中最左邊序號的不同,下面是正確的json資料,體會什麼叫以新的一行作為結束的標識。

這種操作實際上是id存在就更新,不存在就建立,返回結果如下:

{
  "took": 162,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "test01",
        "_type": "doc01",
        "_id": "1",
        "_version": 6,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 5,
        "_primary_term": 1,
        "status": 200
      }
    },
    {
      "index": {
        "_index": "test01",
        "_type": "doc01",
        "_id": "2",
        "_version": 2,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1,
        "status": 200
      }
    },
    {
      "index": {
        "_index": "test01",
        "_type": "doc01",
        "_id": "3",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 201
      }
    }
  ]
}

檢視下這三個id的具體內容:
image

5、批量修改
引數:

{"update":{"_id":"xxx"}}
{"doc": { "XXX": "xxx" } }
{"delete":{"_id":"xxx"}}

現在對上面建立的三條記錄做批量修改:

POST /test01/doc01/_bulk
 {"update":{"_id":"1"}}
{"doc": { "name": "name011"}}
{"delete":{"_id":"2"}}
{"update":{"_id":"3"}}
{"script": "ctx._source.age+=5;ctx._source.name=\"name033\""}  -----------1

和批量建立一樣,最後一行需要換行。
查詢索引test01的資料如下:
image
資料全部修改成功!