
創作人:駱潇龍
Elasticsearch 本着讓使用者友善快捷的使用搜尋功能的原則,對資料定義(索引定義)做了高度抽象,盡可能得避免了重複性定義工作,使之更加靈活。
Elasticsearch 在這方面做的工作主要展現是索引模闆(Index template)和動态映射(Dynamic Mapping)兩個功能。索引模闆的主要功能,是允許使用者在建立索引(index)時,引用已儲存的模闆來減少配置項。操作的一般過程是先建立索引模闆,然後再手動建立索引或儲存文檔(Document)。而自動建立索引時,索引模闆會作為配置的基礎作用。對于 X-Pack 的資料流(Data Stream)功能,索引模闆用于自動建立後備索引(Backing Indices)。該功能的意義是提供了一種配置複用機制,減少了大量重複作勞動。
目前 Elasticsearch 的索引模闆功能以 7.8 版本為界,分為新老兩套實作,新老兩個版本的主要差別是模闆之間複用如何實作。
老版本:
使用優先級(order)關鍵字實作,當建立索引比對到多個索引模闆時,高優先級會繼承并鋪蓋低優先級的模闆配置,最終多個模闆共同起作用。
新版本:
删除了 order 關鍵字,引入了元件模闆 Component template 的概念,是第一段可以複用的配置塊。在建立普通模闆時可以聲明引用多個元件模闆,當建立索引比對到多個新版索引模闆時,取用最高權重的那個。
下面将以生命周期(建立、檢視、使用、删除)為切入點,分别介紹如何使用 Elasticsearch 新老兩個版本的索引模闆。
新版索引模闆
在使用新版本的索引模闆功能前,我們應當确認 Elasticsearch 是否開啟了安全設定,如果是,那麼我們操作的角色對于叢集需要有 manage_index_templates 或 manage 權限才能使用索引模闆功能,這個限制對新老版本都适用。
對于新版,Elasticsearch 為了友善使用者調試,提供了模拟 API 來幫助使用者測試建立索引最終的配置。該模拟 API 主要有 2 個
第一個模拟在現有索引模闆下建立 1 個索引,最終使用的模闆配置是什麼樣的。
第二個模拟在指定模闆的配置下,最終模闆配置是什麼樣的。
第一種模拟 API 使用範例如下:
# 建立1個元件模闆ct1
PUT /_component_template/ct1 # 1
{
"template": {
"settings": {
"index.number_of_shards": 2
}
}
}
# 建立1個元件模闆ct2
PUT /_component_template/ct2 # 2
{
"template": {
"settings": {
"index.number_of_replicas": 0
},
"mappings": {
"properties": {
"@timestamp": {
"type": "date"
}
}
}
}
}
# 建立1個索引模闆 final-template
PUT /_index_template/final-template # 3
{
"index_patterns": ["my-index-*"],
"composed_of": ["ct1", "ct2"],
"priority": 5,
"template":{
"settings": {
"index.number_of_replicas": 1
},
"mappings": {
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}
# 驗證建立名為 my-index-00000 的索引使用的配置是如何
POST /_index_template/_simulate_index/my-index-00000 # 4
#傳回
{
"template" : { # 引用模闆中的配置有settings、mappings、aliase
"settings" : {
"index" : {
"number_of_shards" : "2",
"number_of_replicas" : "1"
}
},
"mappings" : {
"properties" : {
"@timestamp" : {
"type" : "date"
},
"name" : {
"type" : "keyword"
}
}
},
"aliases" : { }
},
"overlapping" : [ # 5
{
"name" : "test-template",
"index_patterns" : ["my-*"]
}
]
}
- 在 #1 處建立了 1 個名為
的元件模闆,該模闆設定了索引分片數為2cr1
- 在 #2 處建立了 1 個名為
的元件模闆,該模闆設定了索引由1個cr2
屬性,并且副本數是0@timestamp
- 在 #3 處建立了 1 個名為
的索引模闆,它适用于所有以final-template
開頭的索引my-index-
- 在 #4 處向
發送 POST 請求,測試建立名為/_index_template/_simulate_index/my-index-00000
的索引,下面的 JSON 是使用索引模闆的配置,可以看出 template 字段是元件 cr1、cr2 以及索引模闆 final-template 全部配置的聚合。my-index-00000
- 在 #5 處的
字段表示忽略了名為overlapping
模闆的配置。test-template
第二種模拟 API 使用範例如下:
# 預設元件模闆 ct1、crt 已建立,内容如前
# 驗證按照該模闆建立的索引時會使用的配置
POST /_index_template/_simulate/<index-template> #1
{ # 2
"index_patterns":["test-*"],
"composed_of": ["ct1","ct2"],
"priority": 5,
"template": {
"settings": {
"index.number_of_replicas": 1
},
"mappings": {
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}
# 傳回
{ # 3
"template" : {
"settings" : {
"index" : {
"number_of_shards" : "2",
"number_of_replicas" : "1"
}
},
"mappings" : {
"properties" : {
"@timestamp" : {
"type" : "date"
},
"name" : {
"type" : "keyword"
}
}
},
"aliases" : { }
},
"overlapping" : [
{
"name" : "test-template",
"index_patterns" : ["my-*"]
}
]
}
- 在 #1 向
發送 POST 請求,其中/_index_template/_simulate/<index-template>
為自定義索引模闆的名詞,該模闆并不會實際建立<index-template>
- 在 #2 處是請求的 body,與建立一個新版索引模闆個格式完全一樣,後續詳細介紹,目前隻需要知道它引用了 cr1 和 cr2 兩個元件模闆
- 在 #3 處為請求傳回的 body,可以看出内容是元件模闆 cr1、cr2 以及請求 body3 個配置的組合。
建立
新版本索引自動配置功能,主要依托元件模闆(component template)和索引模闆(index template)2 個概念來完成。下面依次來介紹他們是如何實作的。
元件模闆的建立
元件模闆是用來建構索引模闆的特殊模闆,它的内容一般是多個索引模闆的公有配置,索引模闆在建立時,可以聲明引用多個元件模闆。
在元件模闆中可配置的索引内容有:别名
aliases
、配置
settings
、映射
mappings
3 個,具體配置方式與建立索引時一緻。元件模闆隻有在被索引模闆引用時,才會發揮作用。當我們需要建立或更新一個元件模闆時,向
/_component_template
路徑發送 PUT 請求即可。
具體示例如下:
# 建立元件模闆
PUT /_component_template/template_1?create=true&master_timeout=30s #1
{
"template": { # 2
"settings": {
"number_of_shards": 1
},
"mappings": {
"_source": {
"enabled": false
},
"properties": {
"name": {
"type": "keyword"
}
}
},
"aliases": {
"test-index": {}
}
},
"version": 1, #3
"_meta": { #4
"description1": "for test",
"description2": "create by phoenix"
}
}
- 在 #1 處向
發送 PUT 請求建立一個名為/_component_template/template_1
的元件模闆,模闆名可以任意替換,需要注意的是,Elasticsearch 中預置有6個元件模闆:template_1
、logs-mappings
logs-settings
metrics-mappings
metrics-settings
synthetics-mapping
,建議不要覆寫。同時 url 中還包含2個可選的查詢參數synthetics-settings
和create
master_timeout
-
,表示此次請求是否是建立請求,如果為 true 則系統中如果已有同名的會報錯,預設為 false,表示請求可以是建立也可能是更新請求create
-
,表示可以容忍的連接配接 Elasticsearch 主節點的時間,預設是30s,如果逾時則請求報錯master_timeout
-
- 在 #2 處
的内容是對索引的設定,主要有别名,映射和配置,在本例中配置了索引分片數是 1,template
不用儲存,索引有名為_source
的屬性,類型是 keyword,同時索引别名有name
。test-index
- 在 #3 處是使用者指定的元件模闆的版本号,為了友善外部管理,此為可選項,預設不會為元件模闆增加版本号
- 在 #4 處是使用者為元件模闆設定的 meta 資訊,該對象字段可随意配置,但不要設定的過大。該字段也是可選的。
索引模闆的建立
建立或更新一個索引模闆的方式都是向
/_index_template
發送 1 個 PUT 請求。索引模闆可以配置的内容主要有 3 類,分别是别名
aliases
settings
mappings
。這 3 部分的配置方式,與建立索引時的設定完全一樣這裡不再贅述。
建立索引模闆的示例:
PUT /_index_template/test_template?create=false&master_timeout=30s #1
{
"index_patterns" : ["te*"], #2
"priority" : 1, #3
"composed_of": ["template_1"], #4
"template": { #5
"settings" : {
"number_of_shards" : 2
}
},
"version": 2, #6
"_meta": { #7
"user": "phoenix",
"time": "2021/05/06"
}
}
# 測試模闆
POST /_index_template/_simulate_index/test #8
{
"template" : {
"settings" : {
"index" : {
"number_of_shards" : "2" # 索引模闆覆寫了元件模闆的配置
}
},
"mappings" : {
"_source" : { # 使用元件模闆的配置
"enabled" : false
},
"properties" : {
"name" : { # 使用元件模闆的配置
"type" : "keyword"
}
}
},
"aliases" : {
"test-index" : { } # 使用元件模闆的配置
}
}
}
- 在 #1 處向
發送 PUT 請求建立索引模闆,模闆名稱為/_index_template/test_template
,名稱可任意填寫,與元件模闆相同,有2個可選的查詢參數:test_template
-
,表示此次請求是否是建立請求,如果為 true 則系統中如果已有同名模闆則會報錯,預設為 false,表示請求可以是建立也可能是更新請求create
-
master_timeout
-
-
字段用于設定比對索引的規則,目前僅支援使用索引名稱比對,支援index_patterns
号作為通配符,該字段是必填字段。需要注意 Elasticsearch 自帶了3種比對規則的索引模闆:*
logs-*-*
metrics-*-*
,建議不要做相同配置。synthetics-*-*
- 在 #3 處
字段配置的是模闆權重,當 1 個索引名符合多個模闆的比對規則時, 會使用該值最大的做為最終使用的模闆,此值預設為 0,Elasticsearch 自帶的模闆此值是100。priority
- 在 #4 處
字段用于配置索引模闆引用的元件模闆,此處引用的元件模闆配置自動會增加到該模闆中。此例我們引用了之前建立的composed_of
元件。template_1
- 在 #5 處
的内容是對索引的設定。template
- 在 #6 處是使用者指定的索引模闆的版本号,為了友善外部管理,此為可選項,預設不會為元件模闆增加版本号
- 在 #7 處是使用者為索引模闆設定的 Meta 資訊,該對象字段可随意配置,但不要設定的過大。該字段也是可選的
- 在 #8 處用模拟 API 建立名為
的索引,我們發現傳回的模闆包含了模闆test
群組件test_template
的所有配置template_1
檢視
建立完索引模闆或元件模闆後,我們可以使用 GET 請求再次檢視其内容,請求是可以指定名稱表示精準找,也可以使用通配符
*
進行範圍查找,如果不指定名稱則會傳回全部。
GET /_component_template/template_1?local=false&master_timeout=30s #1
GET /_component_template/template_* #2
GET /_component_template #3
GET /_index_template/test_template #4
GET /_index_template/test_* #5
GET /_index_template #6
- 在 #1 是檢視名為
的元件模闆template_1
- 在 #2 是檢視名字以
開頭的所有元件模闆template_
- 在 #3 是檢視所有元件模闆,通過該請求我們可以發現 Elasticsearch 預設建立了很多元件模闆,使用時應盡量避免沖突
- 在 #4 是檢視名字為
的索引模闆test_template
- 在 #5 是檢視名字以
開頭的所有索引模闆test_
- 在 #6 是檢視所有索引模闆,通過該請求我們可以發現 Elasticsearch 預設建立了很多索引模闆,使用時應盡量避免沖突
如在 #1 所示,上述所有請求都可以增加 2 個可選的查詢參數:
-
,如果為local
模闆的配置僅從本地節點中擷取,預設為true
表示此次查詢結果是false
節點傳回的master
-
,表示可以容忍的連接配接es主節點的時間,預設是30s,如果逾時則請求報錯master_timeout
使用
關于元件模闆如何使用我們在建立索引模闆時已經提及,增加
composed_of
字段聲明索引模闆引用的元件模闆,這樣元件模闆的配置,就自動增加到了索引模闆中。
索引模闆的使用主要發生在建立索引的時候,如果建立的索引名與索引模闆的
index_patterns
配置相比對,那麼該索引将會在此模闆的基礎上建立。
示例如下:
# 建立名為test-my-template的索引
PUT /test-my-template
{
"settings": {
"index": {
"number_of_replicas": 3
}
},
"mappings": {
"properties": {
"desc":{
"type": "keyword"
}
}
}
}
# 檢視索引資訊
GET /test-my-template
# 傳回
{
"test-my-template" : {
"aliases" : { # 元件模闆 template_1 的配置
"test-index" : { }
},
"mappings" : {
"_source" : { # 元件模闆 template_1 的配置
"enabled" : false
},
"properties" : {
"desc" : { # 建立索引時的配置
"type" : "keyword"
},
"name" : { # 元件模闆 template_1 的配置
"type" : "keyword"
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "3", # 覆寫了 索引模闆 number_of_shards=2 的配置
"provided_name" : "test-my-template",
"creation_date" : "1620550940095",
"number_of_replicas" : "1",
"uuid" : "7UZKxK56SZ-bAwBeDL-Jvg",
"version" : {
"created" : "7100099"
}
}
}
}
}
本例我們建立了名為
test-my-template
的索引,其索引名以
te
開頭,比對索引模闆
test_template
的篩選規則。我們在建立時僅指定了分片數和一個
desc
屬性,而在我們使用 GET 請求檢視索引資訊時可以看出,索引模闆中的别名配置、
_source
配置和
name
屬性的配置被使用了。但是關于分片的設定,由于建立索引時明确指出了,是以模闆中的配置
number_of_shards=2
被覆寫為了 3。
由于在向 一個不存在的索引中插入資料時,Elasticsearch 會自動建立索引,如果新的索引名比對某個索引模闆的話,也會以模闆配置來建立索引。在使用該功能前,請確定 Elasticsearch 叢集的
action.auto_create_index
配置是允許你自動建立目标索引的。
# 向不存在的 test 索引中儲存1條資料
PUT /test/_doc/1
{
"name":"tom",
"age": 18
}
# 檢視 test 索引資訊
GET /test
{
"test" : {
"aliases" : { # 元件模闆 template_1 的配置
"test-index" : { }
},
"mappings" : {
"_source" : { # 元件模闆 template_1 的配置
"enabled" : false
},
"properties" : {
"age" : { # ES 自動推斷建立的屬性
"type" : "long"
},
"name" : { # 元件模闆 template_1 的配置
"type" : "keyword"
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "2", # 使用索引模闆的配置
"provided_name" : "test",
"creation_date" : "1620551933756",
"number_of_replicas" : "1",
"uuid" : "Oop40MPySjKbSKvs0OQwTA",
"version" : {
"created" : "7100099"
}
}
}
}
}
如上例所示,我們向不存在的
test
索引中儲存 1 條資料,該索引名與索引模闆
test_template
的規則相比對。是以索引的配置與模闆配置完全契合,除了模闆中沒有配置的
age
字段,是通過 Elasticsearch 屬性自動建立功能生成的。
删除
當索引模闆或元件模闆完成了它們的使命,我們應該及時将其删除,避免因遺忘導緻建立出的索引出現了預料之外的配置。删除操作非常簡單,發送 DELETE 請求即可,删除同樣可以使用通配符
*
一次删除多個,示例如下:
DELETE /_component_template/template_1?master_timeout=30s&timeout=30s #1
DELETE /_component_template/template_* #2
DELETE /_index_template/test_template #3
DELETE /_index_template/test_* #4
- 在 #1 表示删除名為
template_1
- 在 #2 表示删除名字以
開頭的元件模闆template_
- 在 #3 表示删除名為
test_template
- 在 #3 表示删除名字以
開頭的索引模闆test_
-
,表示可以容忍的等待響應時間,預設是 30s,如果逾時則請求報錯timeout
-
,表示可以容忍的連接配接 Elasticsearch 主節點的時間,預設是 30s,如果逾時則請求報錯master_timeout
老版索引模闆
Elasticsearch 7.8 版本之前的索引模闆功能,與新版本基本相同,唯一的差別就是在模闆的複用方式上。老版本允許在建立索引時,比對到多個模闆,多個模闆間根據
order
配置的優先級從低到高依次覆寫。這種方式會造成使用者在建立索引時,不能明确知道自己到底用了多少模闆,索引配置在繼承覆寫的過程中非常容易出錯。
下面依然從模闆的生命周期出發,介紹如何使用。
與新版本一樣,建立或更新一個老版索引模闆,隻需要向
/_template
發送PUT請求即可,通過索引模闆可配置的字段依然是:别名
aliases
settings
mappings
3個。
# 建立或更新老模闆
PUT /_template/old_template?order=1&create=false&master_timeout=30s # 1
{
"index_patterns": ["te*", "bar*"], #2
"settings": { #3
"number_of_shards": 1
},
"aliases": { #4
"old-template-index": {}
},
"mappings": { #5
"_source": {
"enabled": false
},
"properties": {
"host_name": {
"type": "keyword"
}
}
},
"version": 0 #6
}
-
發送PUT請求建立或索引模闆,模闆名稱為/_template/old_template
,名稱可任意填寫,該請求有 4 個可選的查詢參數:old_template
-
,表示此次請求是否是建立請求,如果為 true 則系統中如果已有同名模闆會報錯,預設為 false,表示請求可以是建立也可能是更新請求create
-
master_timeout
-
,該變量接受一個整數,表示模闆的優先級,數字越大優先級越高,相關配置越可能被實際使用,強烈建議每個模闆都根據實際情況配置該值,不要使用預設值。order
-
- 在 #2 處
字段用于配置比對索引的規則,目前僅支援使用索引名稱比對,支援index_patterns
号作為通配符,該字段是必填字段,可配置多個值表示”或“的關系。*
- 在 #3 處
字段用于配置索引屬性。settings
具體規則詳見文檔: https://www.elastic.co/guide/en/elasticsearch/reference/7.10/index-modules.html#index-modules-settings
- 在 #4 處
字段用于配置索引的别名。aliases
https://www.elastic.co/guide/en/elasticsearch/reference/7.10/indices-aliases.html
-
字段用于配置索引的映射。mappings
具體規則詳見文檔: https://www.elastic.co/guide/en/elasticsearch/reference/7.10/mapping.html
- 在 #6 處
字段是使用者指定的索引模闆的版本号,為了友善外部管理,此為可選項,Elasticsearch 預設不會為元件模闆增加版本号。version
我們可以使用 GET 請求,檢視其模闆内容,同樣可以使用名稱精确查找,也可以使用通配符部分搜尋以及搜尋全部。老版本還支援使用
HEAD
請求快速驗證 1 個模闆是否存在
GET /_template/old_template?local=false&master_timeout=30s&flat_settings=false #1
GET /_template/old_* #2
GET /_template #3
HEAD /_template/old_template #4
-
old_template
-
old_
- 在 #3 是檢視所有索引模闆,通過該請求我們可以發現 Elasticsearch 預設建立了很多元件模闆,使用時應盡量避免沖突
- 在 #4 是驗證名為
的索引模闆是否存在,與上面 3 個請求傳回模闆内容不同,本請求不傳回模闆内容,以狀态碼為 200 表示存在,404 表示不存在old_template
如在#1所示,上述所有請求都可以增加 3 個可選的查詢參數:
-
local
元件模闆的配置僅從本地節點中擷取,預設為true
false
master
-
master_timeout
-
,表示傳回的配置中關于flat_settings
字段如何展示,如果為 false 則使用标準 JSON 格式展示,預設為 true 表示多級屬性會壓縮成 1 級屬性名,以點分格式展示,比如關于索引分片的設定,如果該變量為 true 則傳回為settings
index.number_of_shards:1
索引模闆的使用,主要發生在建立索引的時候,如果建立的索引名與索引模闆相比對,那麼該索引将會在此模闆的基礎上建立。需要注意的是,如果同時比對到新老兩個版本的模闆,那麼預設使用新版本。如果僅比對到多個老版模闆則根據
order
字段依次覆寫。
# 建立索引
PUT /bar-test-old
{
"settings": {
"number_of_replicas": 2
},
"mappings": {
"_source": {
"enabled": true
},
"properties": {
"ip":{
"type": "ip"
}
}
}
}
# 檢視索引
GET /bar-test-old
# 傳回
{
"bar-test-old" : {
"aliases" : {
"old-template-index" : { } # old_template模闆中的配置
},
"mappings" : {
"_source": {
"enabled": true # 建立時的配置覆寫模闆的配置
},
"properties" : {
"host_name" : { # old_template模闆中的配置
"type" : "keyword"
},
"ip" : {
"type" : "ip"
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1", # old_template模闆中的配置
"provided_name" : "bar-test-old",
"creation_date" : "1620615937000",
"number_of_replicas" : "2",
"uuid" : "jG3xiiB_S-iFrlwZ7df56g",
"version" : {
"created" : "7100099"
}
}
}
}
}
bar-test-old
bar
old_template
的篩選規則,我們在建立時僅指定了副本數和 1 個
ip
屬性,而使用 GET 請求檢視索引資訊時可以看出,索引模闆中的别名配置、
host_name
屬性和分片數的配置被使用了。但是關于
_source
的設定,由于建立索引時明确指出了,是以模闆中的
false
配置被覆寫為了
true
當比對到多個老版索引模闆時,最終配置為多個模闆的組合,當不同模闆配置了相同字段時,那麼以
order
高的模闆配置為準。示例如下:
# 建立新版索引模闆
PUT /_index_template/new_template #1
{
"index_patterns": ["template*"],
"priority" : 100,
"template": {
"mappings": {
"dynamic": "true"
},
"aliases": {
"new-template": {}
}
}
}
# 建立老版索引模闆,不配置 order
PUT /_template/old_template_1 #2
{
"index_patterns": ["temp*"],
"mappings": {
"dynamic": "false"
},
"aliases": {
"old_template_1": {}
}
}
# 建立老版本索引模闆,配置高 order
PUT /_template/old_template_2?order=10 #3
{
"index_patterns": ["temp-*"],
"mappings": {
"dynamic": "strict"
},
"aliases": {
"old_template_2": {}
}
}
# 建立索引,名稱會比對 new_template 模闆和 old_template_1 模闆
PUT /template-test #4
{
"settings": {
"number_of_shards": 2
},
"mappings": {
"properties": {
"ip":{
"type": "ip"
}
}
}
}
# 檢視索引配置
GET /template-test #5
# 傳回
{
"template-test" : {
"aliases" : {
"new-template" : { } # 僅有新版本模闆的别名設定
},
"mappings" : {
"dynamic" : "true",
"properties" : {
"ip" : {
"type" : "ip"
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "2",
"provided_name" : "template-test",
"creation_date" : "1620617781794",
"number_of_replicas" : "1",
"uuid" : "0PKYANozRya9zG3LdMG1UA",
"version" : {
"created" : "7100099"
}
}
}
}
}
# 建立索引,名稱會比對 old_template_1 模闆和 old_template_1 模闆
PUT /temp-test #6
{
"settings": {
"number_of_shards": 2
},
"mappings": {
"properties": {
"ip":{
"type": "ip"
}
}
}
}
# 檢視索引配置
GET /temp-test
# 傳回
{
"temp-test" : {
"aliases" : {
"old_template_1" : { }, # old_template_1 的配置
"old_template_2" : { } # old_template_2 的配置
},
"mappings" : {
"dynamic" : "strict", # old_template_2 的配置
"properties" : {
"ip" : {
"type" : "ip"
}
}
},
"settings" : {
"refresh_interval" : "10s",
"number_of_shards" : "2",
"provided_name" : "temp-test",
"creation_date" : "1620617979932",
"number_of_replicas" : "1",
"uuid" : "RwvrdGziT7iVmVutXYPwqA",
"version" : {
"created" : "7100099"
}
}
}
}
}
- 在 #1 處我們建立了 1 個新版本的索引模闆比對名字,以
開頭的索引,設定可以動态增加屬性并設定别名template
new-template
- 在 #2 處我們建立了 1 個老版本的索引模闆比對名字,以
開頭的索引,設定不動态增加屬性并設定别名temp
old_template_1
- 在 #3 處我們建立了 1 個老版本的索引模闆比對名字,以
開頭的索引,設定有新增屬性時報錯并設定别名temp-
,同時配置old_template_2
為10order
- 在 #4 處我們建立了 1 個名為
的索引template-test
- 在 #5 處檢視索引
的配置,該名稱會比對新版template-test
模闆,和老版new_template
模闆,根據索引的别名資訊,隻有新版模闆配置的别名可以看出,該索引僅僅應用了old_template_1
模闆new_template
- 在 #6 處建立了 1 個名為
temp-test
- 在 #7 處檢視索引
的配置,該名稱會比對老版temp-test
old_template_1
模闆,根據索引的别名資訊有old_template_2
old_template_1
可以看出,2 個模闆的配置都應用了。通過old_template_1
字段為dynamic
我們可以判斷該字段使用了strict
較高的模闆order
的配置old_template_2
老版本的索引模闆完成使命後,應該及時将其删除,避免建立索引時比對到不必要的模闆,導緻最終建立的索引與預期不符。删除操作非常簡單,發送 delete 請求即可,删除同樣可以使用通配符
*
DELETE /_template/old_template_1?master_timeout=30s&timeout=30s #1
DELETE /_template/old_template* #2
-
old_template_1
-
old_template
- 在 如 #1 所示,上述 2 個請求都可以增加 2 個可選的查詢參數
-
timeout
-
,表示可以容忍的連接配接es主節點的時間,預設是 30s,如果逾時則請求報錯master_timeout
注意事項及技巧
目前新老版本的索引模闆在7.10版本都可以使用,并且都是通過名稱比對的方式,來決定最後使用的配置,那麼在使用的過程中難免會遇到,比對到多個模闆且配置有沖突的情況。下面總結幾點規則來介紹,建立索引的最終配置是如何決定的。
- 如果同時比對到新老 2 個版本的索引模闆,那麼使用新版模闆。
- 如果僅比對到多個新版模闆,那麼使用
值最高的索引模闆。priority
- 如果新版模闆中配置了多個元件模闆,且元件中有配置沖突,那麼使用
數組中靠後的元件模闆的配置。composed_of
- 如果元件模闆和索引模闆有字段沖突,那麼使用索引模闆中的配置。
- 如果僅比對到多個老版模闆,那麼最終配置由多個模闆共同構成,如果有配置沖突,使用
值高的模闆的配置。order
- 如果建立索引語句中的配置與索引模闆(不管新老版本)沖突,那麼使用建立語句中的配置。
索引模闆一般和動态映射結合使用,這樣可以大大減少建立索引的語句,縮減索引建立頻次。配置方式是在
mappings
字段中配置
dynamic_templates
的相關内容。
這個功能一般用于建立時序類資料的索引,比如日志資料,每天都會有新資料進入索引,資料量會持續增加,隻用一個索引肯定不合适,需要按日或按月建立。
這種情況約定錄入資料的索引名稱與日期相關,再建立索引模闆,這樣資料持續錄入時,索引也會按需增加,且不用人工幹預,後期對不同索引還能做冷熱處理。
創作人簡介:
駱潇龍,進階 Java 開發工程師,關注大資料技術領域。
部落格:
https://blog.gaiaproject.club/