【基礎篇】elasticsearch之索引模板Template[轉]
一,模板簡述: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
二,建立模板:
例如:
{
"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"
}
}
}
}
}
三,刪除模板:
DELETE /_template/template_1
四,檢視模板:
GET /_template/template_1
也可以通過模糊匹配得到多個模板資訊
GET /_template/temp*
可以批量檢視模板
GET /_template/template_1,template_2
驗證模板是否存在:
HEAD _template/template_1
五:多個模板同時匹配,以order順序倒排,order越大,優先順序越高
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 }
}
}
}
六,模板版本號:
模板可以選擇新增版本號,這可以是任何整數值,以便簡化外部系統的模板管理。版本欄位是完全可選的,它僅用於模板的外部管理。要取消設定版本,只需替換模板即可
建立模板:
PUT /_template/template_1
{
"template" : "*",
"order" : 0,
"settings" : {
"number_of_shards" : 1
},
"version": 123
}
檢視模板版本號:
GET /_template/template_1?filter_path=*.version
響應如下:
{
"template_1" : {
"version" : 123
}
}