Amazon Elasticsearch Service(以下Amazon ES)には以下のマイナーな制約がある。
- バージョン7.8でテンプレートパスが
_index_template
に変更されたが、Amazon ESでは現状、従来通りの_template
のみサポート。-
OpenDistroでは
_index_template
となっているので若干紛らわしい。
-
OpenDistroでは
- また、デフォルトのシャード数(
number_of_shards
)もバージョン7以降1に変更されたが、AESでは従来の5に維持されている。
これらを踏まえて、(諸々のAPI操作の備忘録も兼ねて)7.9でのインデックステンプレートの作成方法についてメモ。
参考資料:
サポートされているElasticsearchオペレーション
OpenDistro Create Template構文
(2020) Amazon Elasticsearch Service ドメインを設定するベストプラクティス
(2017) Get Started with Amazon Elasticsearch Service: How Many Shards Do I Need?
やってみる
- 今あるインデックステンプレートを確認する。
- zshだと
?pretty
は使えないようなので、最初にbashに変更する。 - または、
コマンド | jq
でもいい(ターミナルによってはこっちの方が見やすいかも)。
- zshだと
現在のテンプレートの確認
% bash
$ set -o vi
$ curl -X GET https://<文字列>.ap-northeast-1.es.amazonaws.com/_template?pretty
結果
{ }
現在は空の状態。
- デフォルトの挙動を確認してみる。
テストインデックスの作成
$ curl -X PUT https://<文字列>.ap-northeast-1.es.amazonaws.com/hogeindex?pretty
結果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "hogeindex"
}
インデックス設定の確認
$ curl -X GET https://<文字列>.ap-northeast-1.es.amazonaws.com/hogeindex/_settings?pretty
結果
{
"hogeindex" : {
"settings" : {
"index" : {
"creation_date" : "1613367421242",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "hhnRHWONT12UFU9vO7R6kg",
"version" : {
"created" : "7090199"
},
"provided_name" : "hogeindex"
}
}
}
}
number_of_shards
が5に設定されていることがわかる。
- インデックステンプレート用の設定ファイルを準備する。
- シャードは無闇に増やさないの原則に則って、
number_of_shards
を1に設定する。 - この辺はユースケースによって要件が変わる。日次で新たなインデックスを作るログ系を念頭に、インデックス名が
log-*
のパターンにマッチすることを条件とする。
- シャードは無闇に増やさないの原則に則って、
shard-settings.json
{
"template": "log-*",
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
}
- インデックステンプレートを
shard-settings
という名前で作成する。
インデックステンプレートの作成
$ curl -X PUT https://<文字列>.ap-northeast-1.es.amazonaws.com/_template/shard-settings?pretty -H 'Content-Type: application/json' -d @shard-settings.json
結果
{
"acknowledged" : true
}
- 設定を確認する。
作成したインデックステンプレートの確認
$ curl -X GET https://<文字列>.ap-northeast-1.es.amazonaws.com/_template/shard-settings?pretty
結果
{
"shard-settings" : {
"order" : 0,
"index_patterns" : [
"log-*"
],
"settings" : {
"index" : {
"number_of_shards" : "1",
"number_of_replicas" : "1"
}
},
"mappings" : { },
"aliases" : { }
}
}
- 動作を確認する。
テスト用インデックスの作成
$ curl -X PUT https://<文字列>.ap-northeast-1.es.amazonaws.com/log-20210215?pretty
結果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "log-20210215"
}
インデックス設定の確認
$ curl -XGET https://<文字列>.ap-northeast-1.es.amazonaws.com/log-20210215?pretty
結果
{
"log-20210215" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1613369519933",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "X6rtBngiRHy1RhcB7gvSGw",
"version" : {
"created" : "7090199"
},
"provided_name" : "log-20210215"
}
}
}
}
log1-...のインデックス命名規則に基づき、無事number_of_shards
が1に設定された。
以降、日次で作成される同様のプレフィックスに基づくログ用インデックスにも、同様の設定が適用されることになる(ここでは冗長になるので省略するが、命名規則が一致しないインデックスはシャード数=5で作成される)。
掃除
- 作った設定の掃除(結果は省略) 。
インデックスの削除
$ curl -X DELETE https://<文字列>.ap-northeast-1.es.amazonaws.com/log-20210215?pretty
$ curl -X DELETE https://<文字列>.ap-northeast-1.es.amazonaws.com/hogeindex?pretty
作成したインデックステンプレートの削除
$ curl -X DELETE https://<文字列>.ap-northeast-1.es.amazonaws.com/_template/shard-settings?pretty