Elasticsearchのインデックステンプレートのpriorityで優先度を変えてみる

重複した内容のインデックステンプレートを作成する場合について、少しだけ調べてみました。

サマリ

priorityで設定された値が大きいものが、適用されます。

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-template.html

環境

次のDockerComposeファイルを使って、elasticsearchを起動しました。

version: '2.2'
services:
  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2
    ports:
      - 9200:9200
      - 9300:9300
    environment:
      - discovery.type=single-node
      - action.destructive_requires_name=true
    networks:
      - elastic
  ki01:
    image: docker.elastic.co/kibana/kibana:7.16.2
    environment:
      ELASTICSEARCH_HOSTS: "http://es01:9200"
    ports:
      - 5601:5601
    networks:
      - elastic
networks:
  elastic:
    driver: bridge

インデックステンプレートを2つ作り、priorityの動作を確かめる

1つ目のインデックステンプレートを作ります。priorityの指定がない場合、priotiyは最低値の0と解釈されます。

priorityの値がelasticsearchで自動的に生成されることはありません。

こちらのインデックステンプレートには、hostをtextとしてマッピングします。

PUT /_index_template/my_template_1
{
    "index_patterns": "accesslog-*",
    "template": {
        "mappings": {
            "properties": {
                "host": {
                    "type": "text"
                }
            }
        }
    }
}

2つ目のインデックステンプレートを作ってみます。あえて、priorityを指定せず、同じインデックスパターンで作成をしてみます。

PUT /_index_template/my_template_2
{
    "index_patterns": "accesslog-*",
    "template": {
        "mappings": {
            "properties": {
                "host": {
                    "type": "keyword"
                }
            }
        }
    }
}

すると、同じ条件のインデックスパターンで、同じpriorityのインデックステンプレートは作成できないと言われます。

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "index template [my_template_2] has index patterns [accesslog-*] matching patterns from existing templates [my_template_1] with patterns (my_template_1 => [accesslog-*]) that have the same priority [0], multiple index templates may not match during index creation, please use a different priority"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "index template [my_template_2] has index patterns [accesslog-*] matching patterns from existing templates [my_template_1] with patterns (my_template_1 => [accesslog-*]) that have the same priority [0], multiple index templates may not match during index creation, please use a different priority"
    },
    "status": 400
}

と、言うわけで、priorityを高くしてもう一度リクエストを飛ばし、作成します。

こちらのインデックステンプレートには、hostをkeywordとしてマッピングします。

PUT /_index_template/my_template_2
{
    "index_patterns": "accesslog-*",
    "template": {
        "mappings": {
            "properties": {
                "host": {
                    "type": "keyword"
                }
            }
        }
    },
    "priority": 1
}

priorityが大きいインデックステンプレートが使われるか確かめる

インデックスを作成し、どちらのインデックステンプレートが作成されるか確かめます。

PUT /accesslog-20220117

GET /accesslog-20220117/_mapping
{
    "accesslog-20220117": {
        "mappings": {
            "properties": {
                "host": {
                    "type": "keyword"
                }
            }
        }
    }
}

hostがkeywordとなっているので、priorityが大きいmy_template_2が適用されたことがわかりました。