~~NOCACHE~~ ## 3.Template Templateはindex_patternsにマッチしたインデックス作成やドキュメント登録の際に、Templateに従って作成/登録される。 {{:Aws:ElasticSearch:pasted:20210726-115632.png?direct 800x0}} ### 最低限必要な書式 最低限必要な書式は[index_patterns]のみです。([index_patterns]のみだと何も定義されていない状態ですが・・・) ### 大項目 他にもあると思いますが、よく使うものだけ。 #### index_patterns 一致させるインデックスをワイルドカード式で記載します。 { "index_patterns" : [ "{index名}*" ] } #### order インデックスが複数のテンプレートに一致する場合にElasticsearchがこのテンプレートを適用する順序。 order値が小さいテンプレートが最初にマージされます。order値が大きいテンプレートは後でマージされ、値が小さいテンプレートが上書きされます。 なるほど・・・複雑なことができそうではありますが、マッチするindex_patternsは1つにしておいた方が良いですね。 "order" : {数値} #### version テンプレートの管理番号、自身でバージョン管理したい時に利用する。 "version" : {数値} #### settings インデックスの構成オプション。シャード数やレプリカ数などを指定する。 https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings ■記載例 "settings" :{ "index": { "refresh_interval": "5s", "number_of_shards": "1", "number_of_replicas": "0", "analysis": { "analyzer": { "custom_analyzer": { "filter": [ "kuromoji_baseform", "kuromoji_part_of_speech", "ja_stop", "kuromoji_number", "kuromoji_stemmer" ], "char_filter": [ "icu_normalizer", "kuromoji_iteration_mark" ], "type": "custom", "tokenizer": "kuromoji_tokenizer" } } } } } #### mappings インデックス内のフィールドのマッピング。 https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html ##### dynamic_templates フィールドに対しマッピングルールを事前登録しておくことで、未知のフィールドに対して動的に定義を適用してくれるらしい。 理解したら追記する項目。 ■記載例 { "dynamic_templates": [ { "message_field": { "path_match": "message", "mapping": { "norms": false, "type": "text" }, "match_mapping_type": "string" } }, { "string_fields": { "mapping": { "norms": false, "type": "text", "fields": { "keyword": { "ignore_above": 8191, "type": "keyword" } } }, "match_mapping_type": "string", "match": "*" } } ] } ##### properties(ExplicitMapping) フィールドに対しマッピングルールを事前登録しておくことで、フィールドに対して静的に定義を適用する。 フィールドの値についてAnalyzeしたいとか、Dataのフォーマットを指定したいとかtext型とkeword型を使用したいといった目的がある場合に指定する。 ■記載例 "properties" : { "@timestamp": { "type": "date" }, "@version": { "type": "keyword" }, "attachment": { "type": "object", "properties": { "content": { "analyzer": "custom_analyzer", "type": "text", "fields": { "keyword": { "ignore_above": 8191, "type": "keyword" } } } } } } ### Template全体例 { "order": 0, "version": 1, "index_patterns": "Test*", "settings": { "index": { "refresh_interval": "5s", "number_of_shards": "1", "number_of_replicas": "0", "analysis": { "analyzer": { "custom_analyzer": { "filter": [ "kuromoji_baseform", "kuromoji_part_of_speech", "ja_stop", "kuromoji_number", "kuromoji_stemmer" ], "char_filter": [ "icu_normalizer", "kuromoji_iteration_mark" ], "type": "custom", "tokenizer": "kuromoji_tokenizer" } } } } }, "mappings": { "dynamic_templates": [ { "message_field": { "path_match": "message", "mapping": { "norms": false, "type": "text" }, "match_mapping_type": "string" } }, { "string_fields": { "mapping": { "norms": false, "type": "text", "fields": { "keyword": { "ignore_above": 8191, "type": "keyword" } } }, "match_mapping_type": "string", "match": "*" } } ], "properties": { "@timestamp": { "type": "date" }, "@version": { "type": "keyword" }, "attachment": { "type": "object", "properties": { "content": { "analyzer": "custom_analyzer", "type": "text", "fields": { "keyword": { "ignore_above": 8191, "type": "keyword" } } } } } } } } {{tag>AWS ElasticSearch}}