いままでElasticsearchのシングルindexでデータをためていたが、どんどん肥大化していき古いデータの更新や本番環境へのsnapshot/restoreがそろそろ辛くなってきたため、日付ごとにindexを変えてためる運用を始めた
よくログとかではこのパターンを使うようで、例えば myapp_logs_index_07_11_2019
、 myapp_logs_index_08_11_2019
のような感じで日付を末尾につけるやり方
ただ、複数のindexを操作したい場合、以前のような1つのindexを操作する場合とは勝手が少し違ったのでやり方をまとめてみた
※実際にはログの場合と少し違い、データに種別があってmy-index-1-tigers-2020.03.15
, my-index-1-giants-2020.03.15
, my-index-1-swallows-2020.03.15
のように -{種別}-{日付}
が末尾につくindex名にしている
基本操作
検索
■ シングル index パターン
GET my-index-1/item/_search
■ デイリー index パターン
指定した期間のindexをカンマでつなげる
GET my-index-1-*2020.03.15,my-index-1-*2020.03.16,my-index-1-*2020.03.17/item/_search
エイリアスをはる
■ シングル index パターン
POST /_aliases { "actions" : [ {"add": {"index": "my-index-1", "alias": "my-index" }} ] }
■ デイリー index パターン
指定した期間のindexをactionsにセットする
POST /_aliases { "actions" : [ {"add": {"index": "my-index-1-*2020.03.15", "alias": "my-index" }}, {"add": {"index": "my-index-1-*2020.03.16", "alias": "my-index" }}, {"add": {"index": "my-index-1-*2020.03.17", "alias": "my-index" }} ] }
SNAPSHOTをとる
■ シングル index パターン
PUT /_snapshot/my_gcs_repository/myapp-20200317-1 { "indices": [ "my-index-1" ], "ignore_unavailable": true, "include_global_state": false }
■ デイリー index パターン
指定した期間のindex名をindicesにセットする
PUT /_snapshot/my_gcs_repository/myapp-20200317-1 { "indices": [ "my-index-1-*2020.03.15", "my-index-1-*2020.03.16", "my-index-1-*2020.03.17" ], "ignore_unavailable": true, "include_global_state": false }
RESTOREする
■ シングル index パターン
PUT /_snapshot/my_gcs_repository/myapp-20200317-1 { "indices": [ "my-index-1" ], "ignore_unavailable": true, "include_global_state": false }
■ デイリー index パターン
snapshotしたindexをすべてrestoreしたいので「*」でくくってもよいはず
aliasを貼ったままにしておくかどうかはまだわからないので一旦falseにしておく
POST /_snapshot/my_gcs_repository/myapp-20200317-2/_restore?pretty { "indices": "my-index-1-*", "ignore_unavailable": true, "include_global_state": false, "include_aliases": false }
削除
■ シングル index パターン
DELETE my-index-1
■ デイリー index パターン
指定した期間のindexをカンマでつなげる
DELETE my-index-1-*2020.03.15,my-index-1-*2020.03.16,my-index-1-*2020.03.17