Elasticsearchのcomponent templateは左から順に適用される

サマリ

component templateは、index templateに配列で指定されますが、左から順に適用されます。

適用の動作は各フィールドを合成するイメージです。重複するフィールドの場合は値が上書きされ、重複するフィールドが無ければ追加されます。

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

環境

前回の記事と同様の環境です。

https://baubaubau.hatenablog.com/entry/2022/01/21/230451

また、RESTクライアントとして、vscodeの以下のプラグインを使いました。

https://github.com/hsen-dev/vscode-elastic

二つのcomponent templateを作成する

次の通り、二つのcomponent templateを作成します。

それぞれ、フィールドには重複するものと、しないものをあえて記述します。hostは両方に記述されますが、methodとurlは片方にしか記述がありません。

PUT _component_template/my_component_template_1
{
    "template": {
        "mappings": {
            "properties": {
                "host": {
                    "type": "keyword"
                },
                "method": {
                    "type": "keyword"
                }
            }
        }
    }
}

PUT _component_template/my_component_template_2
{
    "template": {
        "mappings": {
            "properties": {
                "host": {
                    "type": "text"
                },
                "url": {
                    "type": "keyword"
                }
            }
        }
    }
}

これらのcomponent templateを使って、index templateを作成します。

PUT _index_template/my_index_template
{
    "index_patterns": ["access*"],
    "composed_of": ["my_component_template_1", "my_component_template_2"]
}

indexを作成しmappingを確認すると、component templateが指定された順番に適用されていることがわかります。

PUT accesslog-20220101/

GET accesslog-20220101/_mapping

{
    "accesslog-20220101": {
        "mappings": {
            "properties": {
                "host": {
                    "type": "text"
                },
                "method": {
                    "type": "keyword"
                },
                "url": {
                    "type": "keyword"
                }
            }
        }
    }
}

重複して指定されていたhostフィールドは左から順に合成されたため、typeがtextになっています。

それ以外のフィールドは重複していないため、指定したtype通りに設定されています。