Elasticsearch 參考指南(索引模板)

索引模板

索引模板容許你定義在建立新索引時自動應用的模板,模板包括設置和映射,以及一個簡單的模式模板,該模板控制是否應該將模板應用於新索引。app

模板只在建立索引時應用,更改模板不會對現有索引產生影響,當使用建立索引API時,做爲建立索引調用的一部分定義的設置/映射將優先於模板中定義的任何匹配設置/映射。

例如:版本控制

PUT _template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_source": {
      "enabled": false
    },
    "properties": {
      "host_name": {
        "type": "keyword"
      },
      "created_at": {
        "type": "date",
        "format": "EEE MMM dd HH:mm:ss Z yyyy"
      }
    }
  }
}
索引模板提供c樣式 /* */塊註釋,在JSON文檔中,除了初始左大括號以前,其餘地方都容許使用註釋。

定義一個名爲template_1的模板,模板模式爲te*bar*,設置和映射將應用於任何匹配te*bar*模式的索引名稱。code

也能夠在索引模板中包含如下別名:orm

PUT _template/template_1
{
    "index_patterns" : ["te*"],
    "settings" : {
        "number_of_shards" : 1
    },
    "aliases" : {
        "alias1" : {},
        "alias2" : {
            "filter" : {
                "term" : {"user" : "kimchy" }
            },
            "routing" : "kimchy"
        },
        "{index}-alias" : {} 
    }
}
別名中的 {index}佔位符將被替換爲模板在建立索引期間應用到的實際索引名。

刪除一個模板

索引模板由一個名稱標識(在上面的例子中是template_1),也能夠刪除:對象

DELETE /_template/template_1

獲取模版

索引模板由一個名稱標識(在上面的例子中是template_1),可使用如下方法檢索:索引

GET /_template/template_1

還可使用通配符匹配多個模板,如:文檔

GET /_template/temp*
GET /_template/template_1,template_2

獲取可運行的全部索引模板的列表:io

GET /_template

模板存在

用於檢查模板是否存在,例如:form

HEAD _template/template_1

HTTP狀態碼指示具備給定名稱的模板是否存在,狀態碼200表示存在,404表示不存在。模板

在7.0.0以前,映射定義用於包含類型名稱,雖然默認狀況下映射再也不包含類型名稱,可是仍然能夠經過設置參數 include_type_name使用舊格式。

多模板匹配

多個索引模板可能匹配一個索引,在本例中,設置和映射都合併到索引的最終配置中,可使用order參數控制合併的順序,先應用較低的順序,而後用較高的順序覆蓋它們,例如:

PUT /_template/template_1
{
    "index_patterns" : ["*"],
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "_source" : { "enabled" : false }
    }
}

PUT /_template/template_2
{
    "index_patterns" : ["te*"],
    "order" : 1,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "_source" : { "enabled" : true }
    }
}

上面的操做將禁用存儲_source,可是對於以te*開頭的索引,仍然啓用_source,注意,對於映射,合併是「深度」的,這意味着基於對象/屬性的映射能夠很容易地在高階模板上添加/覆蓋,而低階模板提供了基礎。

具備相同順序值的多個匹配模板將致使不肯定的合併順序。

模板版本控制

爲了簡化外部系統對模板的管理,模板能夠選擇添加一個version號,版本號能夠是任何整數,version字段是徹底可選的,僅用於模板的外部管理,要取消version設置,只需替換模板而不用指定另外一個。

PUT /_template/template_1
{
    "index_patterns" : ["*"],
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "version": 123
}

要檢查版本,可使用filter_path過濾響應,將響應限制爲只有version

GET /_template/template_1?filter_path=*.version

這應該會給出一個小的響應,使解析既簡單又便宜:

{
  "template_1" : {
    "version" : 123
  }
}
相關文章
相關標籤/搜索