ダウンタイムなしでマッピングを変更する (reindex + アトミックなエイリアス切り替え)
Elasticsearchインデックスのライフサイクルコマンド
# 1) Target index with the new mapping (refresh off and replicas 0 for loading):
PUT musteri-v2
{
"settings": { "number_of_shards": 1, "number_of_replicas": 0, "refresh_interval": "-1" },
"mappings": { "properties": { "kod": { "type": "keyword" }, "adet": { "type": "long" } } }
}
# 2) Reindex in the background (tracked with the returned task id):
POST _reindex?wait_for_completion=false&slices=auto
{
"source": { "index": "musteri-v1", "size": 1000 },
"dest": { "index": "musteri-v2", "op_type": "create" },
"conflicts": "proceed"
}
GET _tasks/<task_id>
# 3) Restore settings and compare document counts:
PUT musteri-v2/_settings
{
"index": { "refresh_interval": null, "number_of_replicas": 1 }
}
POST musteri-v2/_refresh
GET _cat/indices/musteri-v1,musteri-v2?v&h=index,docs.count,pri.store.size
# 4) Switch the alias to the new index in one atomic operation:
POST _aliases
{
"actions": [
{ "remove": { "index": "musteri-v1", "alias": "musteri" } },
{ "add": { "index": "musteri-v2", "alias": "musteri", "is_write_index": true } }
]
}概要
新しいマッピングでインデックスを作成し、reindex でバックグラウンドでデータをコピーし、ドキュメント数を比較して、アプリケーションが使用するエイリアスを 1 回のアトミック操作で新しいインデックスに切り替える。
使用する場面
既存フィールドの型を変更する必要がある場合 (text -> keyword、integer -> long)、アナライザーを変更する必要がある場合、またはシャード数を再定義する必要がある場合。これらは既存のインデックス上では直接行えない。
注意
アプリケーションはインデックス名ではなくエイリアスに接続する必要がある。そうでないと、切り替えにデプロイが必要になる。reindex 中にソースへ行われた新しい書き込みはターゲットに届かない: 書き込みを停止するか、切り替えの直前に op_type create + conflicts proceed で reindex を再実行して差分を埋める (この方法では更新と削除は引き継がれない)。reindex はソースに読み取り負荷を、ターゲットに書き込み負荷を生じるため、requests_per_second でスロットルする。