0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Elasticsearch 6.xでupdate時に配列に値を追加する方法

Posted at

ぐぐるとscriptで+= を使う方法ばかりでてくるんですが、 Elasticsearch 6.x だと動かなかったので Elasticsearch 6.xで動く方法をメモしときます。

方法だけ先に書くと下記でいけました。

"script": "ctx._source.フィールド名.add(追加する値)"

サンプルマッピング

PUT sample
{
  "mappings":{
    "sample":{
      "properties":{
        "years":{
          "type": "integer"
        }
      }
    }
  }
}

データ登録

POST sample/sample/1
{
    "years" : [2000]
}

アップデートで2021を追加

POST sample/sample/1/_update
{
  "script" : "ctx._source.years.add(2021)"
}

結果確認

GET sample/sample/1
{
  "_index": "sample",
  "_type": "sample",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "years": [
      2000,
      2021
    ]
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?