1. 程式人生 > >elasticsearch 5.x 系列之四(索引模板的使用,詳細得不要不要的)

elasticsearch 5.x 系列之四(索引模板的使用,詳細得不要不要的)

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