一,模板簡述:template大體分紅setting和mappings兩部分:
索引可以使用預約義的模板進行建立,這個模板稱做Index templates。模板設置包括settings和mappings,經過模式匹配的方式使得多個索引重用一個模板。
1. settings主要做用於index的一些相關配置信息,如分片數、副本數,tranlog同步條件、refresh等。
2. mappings主要是一些說明信息,大體又分爲_all、_source、prpperties這三部分:
(1) _all:主要指的是AllField字段,咱們能夠將一個或多個都包含進來,在進行檢索時無需指定字段的狀況下檢索多個字段。設置「_all" : {"enabled" : true}
(2) _source:主要指的是SourceField字段,Source能夠理解爲ES除了將數據保存在索引文件中,另外還有一份源數據。_source字段在咱們進行檢索時至關重要,若是在{"enabled" : false}狀況下默認檢索只會返回ID, 你須要經過Fields字段去到索引中去取數據,效率不是很高。可是enabled設置爲true時,索引會比較大,這時能夠經過Compress進行壓縮和inclueds、excludes來在字段級別上進行一些限制,自定義哪些字段容許存儲。
(3) properties:這是最重要的步驟,主要針對索引結構和字段級別上的一些設置。
3.我們一般在elasticsearch中 post mapping信息,每從新建立索引便到設置mapping,分片,副本信息。很是繁瑣。強烈建議你們經過設置template方式設置索引信息。設置索引名,經過正則匹配的方式匹配到相應的模板。ps:直接修改mapping的優先級>索引template。索引匹配了多個template,當屬性等配置出現不一致的,以order的最大值爲準,order默認值爲0
二,建立模板:
例如:
html
{
"template": "pmall*", "settings": { "index.number_of_shards": 1, "number_of_replicas": 4, "similarity": { "IgnoreTFSimilarity": { "type": "IgoreTFSimilarity" } } }, "mappings": { "_default_": { "_source": { "enabled": false } }, "commodity": { "properties": { "sold": { "type": "long" }, "online_time": { "type": "long" }, "price": { "type": "long" }, "publish_time": { "type": "long" }, "id": { "type": "long" }, "catecode": { "type": "integer" }, "title": { "search_analyzer": "ikSmart", "similarity": "IgnoreTFSimilarity", "analyzer": "ik", "type": "text" }, "content": { "index": false, "store": true, "type": "keyword" }, "status": { "type": "integer" } } } } }
三,刪除模板:nginx
DELETE /_template/template_1
四,查看模板:
sql
GET /_template/template_1
也能夠經過模糊匹配獲得多個模板信息json
GET /_template/temp*
能夠批量查看模板ruby
GET /_template/template_1,template_2
驗證模板是否存在:
app
HEAD _template/template_1
五:多個模板同時匹配,以order順序倒排,order越大,優先級越高
elasticsearch
PUT /_template/template_1
{
"template" : "*", "order" : 0, "settings" : { "number_of_shards" : 1 }, "mappings" : { "type1" : { "_source" : { "enabled" : false } } } } PUT /_template/template_2 { "template" : "te*", "order" : 1, "settings" : { "number_of_shards" : 1 }, "mappings" : { "type1" : { "_source" : { "enabled" : true } } } }
六,模板版本號:
模板能夠選擇添加版本號,這能夠是任何整數值,以便簡化外部系統的模板管理。版本字段是徹底可選的,它僅用於模板的外部管理。要取消設置版本,只需替換模板便可
建立模板:ide
PUT /_template/template_1
{
"template" : "*", "order" : 0, "settings" : { "number_of_shards" : 1 }, "version": 123 }
查看模板版本號:post
GET /_template/template_1?filter_path=*.version
響應以下:ui
{
"template_1" : { "version" : 123 } }
七,參考:
indices-templates