LoginSignup
2
1

More than 1 year has passed since last update.

Elasticsearchのもやもや: ドキュメントの更新はPUTかPOSTか

Last updated at Posted at 2022-08-26

はじめに

Elasticsearchを使っていて、もやもやした内容を、
メモ書きレベルで、思うがまま書いてみます。

Elasticsearchのドキュメント更新

Elasticsearchのドキュメント更新リクエストは、
PUTメソッドまたはPOSTメソッドを使用します。
その違いや使い分けに、モヤモヤ感が。。。

PUTとPOSTによるドキュメント更新を比較してみた

  • 更新範囲の違い

    PUT test/_doc/1
    {
      "counter" : 1,
      "tags" : ["red"]
    }
    
    • POST: 指定ドキュメントIDを持つドキュメントの一部フィールドを置き換える
      POST test/_update/1
      {
        "doc": {
          "name": "new_name"
        }
      }
      
      • 方法2: script句による更新
      POST test/_update/1
      {
        "script" : {
          "source": "ctx._source.counter += params.count",
          "lang": "painless",
          "params" : {
            "count" : 4
          }
        }
      }
      
  • ドキュメントの再インデックス

    • 両者ともにインデックスが再作成され、ドキュメントのバージョンが上がる

おわりに

Elasticsearchのドキュメント更新方法として、
POSTがスクリプトを使用でき、PUTより柔軟に利用できそうです。

2
1
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
2
1