1,首先看一下下面這個索引模板
curl -XPUT "master:9200/_template/template_1?pretty" -H 'Content-Type: application/json' -d' ---> 模板名字叫做template1
{
"template" : "hello*", -------------> 匹配的索引名字
"order" : 0, -------------> 代表權重,如果有多個模板的時候,優先進行匹配,值越大,權重越高
"settings" : {
"number_of_shards" : 1 -------> 主分片的設置
},
"aliases": {
"alias_1" : {} ------> 索引對應的別名
},
"mappings" : { -----> 字段的映射
"_default_": { -----------------> 默認的配置
"_source" : { "enabled" : false }, ------> 字段原始的具體值是否要存
"_all": { "enabled": false }, -----> 禁用_all 字段,
"dynamic": "strict" ----------> 只可以用定義的字段,關閉默認的自動推斷數據類型
},
"type1" : { ----> 類型名字
"_source" : { "enabled" : true }, ------> 字段具體的值是否要存
"properties": { ---------> 字段映射
"@timestamp": { -------> 具體的字段映射
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"@version": {
"doc_values": true,
"index": "not_analyzed",
"type": "string"
},
"Guid": {
"doc_values": true,
"index": "not_analyzed",
"type": "string"
},
"LogLevel": {
"type": "long"
}
}
}
}
}
'
2,關於索引模板的刪除和查看。
刪除
curl -X DELETE "localhost:9200/_template/template_1"
查看:
GET /_template/template_1
GET /_template/temp*
GET /_template/template_1,template_2
GET /_template
判斷是否存在
HEAD _template/template_1
3,哦了,看完這個簡單的模板之后,我們來注意看一下以下幾點
注意3.1:不要在一個索引中定義多個type。
6.X版本已經不支持,7.X版本徹底不支持。
擴展問題:5.X版本的父子文檔實際實現中是一個索引中定義了多個type,到了6.X中實現方式改變為:join方式。
注意3.2:將Set _source設置為false。
假設你只關心度量結果,不是原始文件內容。
將節省磁盤空間並減少IO。
比如,你可以把原始的數據存儲在mysql ,hbase 等其他地方,從es 中得到id 后,去相應的數據庫中進行取數據
舉例:
“_source”:{
“enabled”:false
},
注意3.3:將_all設置為false。
假設你確切地知道你對哪個field做查詢操作?
能實現性能提升,縮減存儲。
舉例:
“_all”:{
“enabled”:false },
注意3.4:設置dynamic = strict。
假設你的數據是結構化數據。
字段設置嚴格,避免臟數據注入。
舉例:
“dynamic”:”strict”,
注意3.5:使用keyword類型
假設你只關心完全匹配
提高性能和縮小磁盤存儲空間
舉例:
“CLF_CustomerID”:{
“type”:”keyword”
},
4, 索引模板的用途,一般是用在時間序列這樣的索引中
4.1 概述
也就是說,如果你的索引,可以按照每周,或者每天進行建立索引,那么,索引模板的作用就來了。
你只要配置好索引模板,后面你就不用每次都要建立索引的映射了。這個糖果還是很好用的。
4.2 注意的點,索引模板一般配合索引別名一起使用,(下雨天,巧克力和音樂更配哦)
附上官方文檔鏈接,不要太感謝:
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html