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 template 是否存在:
HEAD _template/logs_template
如果存在就會返回:
200 - OK
否則就會返回:
{"statusCode":404,"error":"Not Found","message":"404 - Not Found"}
刪除一個 index template
在之前的練習中,我們匹配 “*”,也就是我們以后所有的創建的新的 index 將不存儲 source,這個顯然不是我們所需要的。我們需要來把這個 template 進行刪除。刪除一個 template 的接口如下:
DELETE /_template/<index-template>
那么針對我們的情況,我們可以使用如下的命令來刪除我們不需要的 template:
DELETE _template/template_1
DELETE _template/template_2
這樣我們刪除了我們剛才創建的兩個 templates。
————————————————
原文鏈接:Elastic 中國社區官方博客