Elasticsearch: Index template


Index template定義在創建新index時可以自動應用的settings和mappings。 Elasticsearch根據與index名稱匹配的index模式將模板應用於新索引。這個對於我們想創建的一系列的Index具有同樣的settings及mappings。比如我們希望每一天/月的日志的index都具有同樣的設置。

Index template僅在index創建期間應用。 對index template的更改不會影響現有索引。 create index API請求中指定的設置和映射會覆蓋索引模板中指定的任何設置或映射。

你可以在代碼中加入像C語言那樣的block注釋。你可以把這個注釋放在出來開頭 “{”和結尾的“}”之間的任何地方。

定義一個Index template

我們可以使用如下的接口來定義一個index template:

PUT /_template/<index-template>

我們可以使用_template這個終點來創建,刪除,查看一個index template。下面,我們來舉一個例子:

    PUT _template/logs_template
    {
      "index_patterns": "logs-*",
      "order": 1, 
      "settings": {
        "number_of_shards": 4,
        "number_of_replicas": 1
      },
      "mappings": { 
        "properties": {
          "@timestamp": {
            "type": "date"
          }
        }
      }
    }

在上面,我們可以看到,我們定義了一個叫做logs_template的index template。它的index_patterns定義為“logs-*”,說明,任何以“logs-”為開頭的任何一個index將具有在該template里具有的settings及mappings屬性。這里的“order”的意思是:如果索引與多個模板匹配,則Elasticsearch應用此模板的順序。該值為1,表明有最先合並,如果有更高order的template,這個settings或mappings有可能被其它的template所覆蓋。

下面,我們來測試一下我們剛定義的index template:

PUT logs-2019-03-01

在這里,我們創建了一個叫做logs-2019-03-01的index。我們使用如下的命令來檢查被創建的情況:

GET logs-2019-03-01

顯示的結果為:

    {
      "logs-2019-03-01" : {
        "aliases" : { },
        "mappings" : {
          "properties" : {
            "@timestamp" : {
              "type" : "date"
            }
          }
        },
        "settings" : {
          "index" : {
            "creation_date" : "1567652671032",
            "number_of_shards" : "4",
            "number_of_replicas" : "1",
            "uuid" : "Dz5rqRS4SEyLM_gf5eEolQ",
            "version" : {
              "created" : "7030099"
            },
            "provided_name" : "logs-2019-03-01"
          }
        }
      }
    }

證如上所示,我們已經成功創建了一個我們想要的index,並且它具有我們之前定義的settings及mappings。

Index template和alias

我們甚至可以為我們的index template添加index alias:

    PUT _template/logs_template
    {
      "index_patterns": "logs-*",
      "order": 1, 
      "settings": {
        "number_of_shards": 4,
        "number_of_replicas": 1
      },
      "mappings": { 
        "properties": {
          "@timestamp": {
            "type": "date"
          }
        }
      },
      "aliases": {
        "{index}-alias" : {}
      }
    }

在上面,我們已經創立了一個叫做{index}-alias的別名。這里的{index}就是實際生成index的文件名來代替。我們下面用一個例子來說明:

PUT logs-2019-04-01

我們創建一個叫做logs-2019-04-01的index, 那么它同時生成了一個叫做logs-2019-04-01-alias的別名。我們可以通過如下的命令來檢查:

GET logs-2019-04-01-alias

顯示的結果是:

    {
      "logs-2019-04-01" : {
        "aliases" : {
          "logs-2019-04-01-alias" : { }
        },
        "mappings" : {
          "properties" : {
            "@timestamp" : {
              "type" : "date"
            }
          }
        },
        "settings" : {
          "index" : {
            "creation_date" : "1567653644605",
            "number_of_shards" : "4",
            "number_of_replicas" : "1",
            "uuid" : "iLf-j_G2T4CYcHCqwz32Ng",
            "version" : {
              "created" : "7030099"
            },
            "provided_name" : "logs-2019-04-01"
          }
        }
      }
    }

Index匹配多個template

多個索引模板可能與索引匹配,在這種情況下,設置和映射都合並到索引的最終配置中。 可以使用order參數控制合並的順序,首先應用較低的順序,並且覆蓋它們的較高順序。 例如:

    PUT /_template/template_1
    {
        "index_patterns" : ["*"],
        "order" : 0,
        "settings" : {
            "number_of_shards" : 1
        },
        "mappings" : {
            "_source" : { "enabled" : false }
        }
    }
     
    PUT /_template/template_2
    {
        "index_patterns" : ["te*"],
        "order" : 1,
        "settings" : {
            "number_of_shards" : 1
        },
        "mappings" : {
            "_source" : { "enabled" : true }
        }
    }

以上的template_1將禁用存儲_source,但對於以te *開頭的索引,仍將啟用_source。 注意,對於映射,合並是“深度”的,這意味着可以在高階模板上輕松添加/覆蓋特定的基於對象/屬性的映射,而較低階模板提供基礎。

我們可以來創建一個例子看看:

    PUT test10
     
    GET test10

顯示的結果是:

    {
      "test10" : {
        "aliases" : { },
        "mappings" : { },
        "settings" : {
          "index" : {
            "creation_date" : "1567654333181",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "iEwaQFl9RAKyTt79PduN-Q",
            "version" : {
              "created" : "7030099"
            },
            "provided_name" : "test10"
          }
        }
      }
    }

如果我們創建另外一個不是以 “te”開頭的index,我們可以看看如下的情況:

    PUT my_test_index
    GET my_test_index

顯示的結果是:

    {
      "my_test_index" : {
        "aliases" : { },
        "mappings" : {
          "_source" : {
            "enabled" : false
          }
        },
        "settings" : {
          "index" : {
            "creation_date" : "1567654713059",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "aSsIZMT2RyWKT44G2dF2zg",
            "version" : {
              "created" : "7030099"
            },
            "provided_name" : "my_test_index"
          }
        }
      }
    }

顯然在mappings里顯示source是被禁止的。

如果對於兩個templates來說,如果order是一樣的話,我們可能陷於一種不可知論的合並狀態。在實際的使用中必須避免。

查詢Index template接口

我們可以通過如下的接口來查詢已經被創建好的index template:

GET /_template/<index-template>

比如:

GET _template/logs_template

顯示的結果是:

    {
      "logs_template" : {
        "order" : 1,
        "index_patterns" : [
          "logs-*"
        ],
        "settings" : {
          "index" : {
            "number_of_shards" : "4",
            "number_of_replicas" : "1"
          }
        },
        "mappings" : {
          "properties" : {
            "@timestamp" : {
              "type" : "date"
            }
          }
        },
        "aliases" : {
          "{index}-alias" : { }
        }
      }
    }

顯示的內容就是我們之前已經創建的那個index template。

你也可以通過如下的方式來同時查詢多個template的情況:

    GET /_template/template_1,template_2
    GET /_template/temp*
    GET /_template

刪除一個index template

在之前的練習中,我們匹配“*”,也就是我們以后所有的創建的新的index將不存儲source,這個顯然不是我們所需要的。我們需要來把這個template進行刪除。刪除一個template的接口如下:

DELETE /_template/<index-template>

那么針對我們的情況,我們可以使用如下的命令來刪除我們不需要的template:

    DELETE _template/template_1
    DELETE _template/template_2

這樣我們刪除了我們剛才創建的兩個templates。

參考:
【1】https://www.elastic.co/guide/en/elasticsearch/reference/7.4/indices-get-template.html
【2】https://www.elastic.co/guide/en/elasticsearch/reference/7.4/indices-delete-template.html
【3】https://www.elastic.co/guide/en/elasticsearch/reference/7.4/indices-templates.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM