索引模板
模板的主要作用:可以幫助簡化創(chuàng)建索引的語句,將模板中的配置和映射應(yīng)用到創(chuàng)建的索引中。
新建索引時,索引名稱滿足index_patterns
條件的,將會使用索引模板中的配置和映射。index_patterns
使用*
進(jìn)行通配,不支持復(fù)雜的正則。
indexPattern
要求:
-
不能包含空字符
-
不能以
_
開頭 -
不能包含以下特殊字符
\ / ? " < > | , #
如果索引匹配了多個索引模板,將通過order
,按升序逐個應(yīng)用和覆蓋相同的配置和映射,order
默認(rèn)值為0,如果多個模板的order
一致,則模板應(yīng)用順序不可控。
模板的管理
創(chuàng)建&修改模板
創(chuàng)建修改模板語法一樣,把修改后的模板在PUT或者POST一下。修改時,模板內(nèi)容是全量覆蓋的。
新建索引模板gudong_1
PUT _template/gudong_1
{
"index_patterns": ["gudong*"],
"order" : 0,
"settings": {
"number_of_shards": 1
},
"mappings": {
"_doc": {
"properties": {
"message": {
"type": "keyword"
},
/* 數(shù)據(jù)插入的時間 */
"create_date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
}
創(chuàng)建索引gudong20211221001
PUT gudong20211221001
查詢索引結(jié)果
{
"gudong20211221001": {
"aliases": {},
"mappings": {
"_doc": {
"properties": {
"create_date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"message": {
"type": "keyword"
}
}
}
},
"settings": {
"index": {
"creation_date": "1640054016128",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "TnVhs-7sQ3Wg-BuqSJUEmQ",
"version": {
"created": "6070299"
},
"provided_name": "gudong20211221001"
}
}
}
}
多模板應(yīng)用
新建索引模板gudong_2
PUT _template/gudong_2
{
"index_patterns": ["gudong*"],
"order" : 2,
"settings": {
"number_of_shards": 1
},
"mappings": {
"_doc": {
"properties": {
"content": {
"type": "keyword"
}
}
}
}
}
創(chuàng)建索引gudong20211221002
PUT gudong20211221002
查詢索引結(jié)果,可以發(fā)現(xiàn)同時應(yīng)用了模板gudong_1和gudong_2
{
"gudong20211221002": {
"aliases": {},
"mappings": {
"_doc": {
"properties": {
"content": {
"type": "keyword"
},
"create_date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"message": {
"type": "keyword"
}
}
}
},
"settings": {
"index": {
"creation_date": "1640054892925",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "Ew8LYOJmRFWMFGs4doZ_Pg",
"version": {
"created": "6070299"
},
"provided_name": "gudong20211221002"
}
}
}
}
查詢模板
查詢所有的模板列表
GET _cat/templates?v&s=name
驗證模板是否存在,通過HTTP狀態(tài)碼來判斷, 200 表示存在,404 表示不存在。
HEAD _template/gudong_1
查詢模板詳細(xì)內(nèi)容
GET _template/gudong_1,gudong_2
查詢所有模板的詳細(xì)內(nèi)容
GET /_template
通配符查詢
GET /_template/gudong_*
刪除模板
單個模板刪除
DELETE /_template/template_1
通配符刪除
DELETE /_template/gudong_*
不支持多個模板名以逗號隔開的方式進(jìn)行刪除,不支持DELETE /_template/template_1,template_2
版本化模板
模板可以選擇添加一個版本號,它可以是任何整數(shù)值,以簡化外部系統(tǒng)對模板的管理。 version 字段是完全可選的,它僅用于模板的外部管理。 要取消設(shè)置版本,只需替換模板而不指定模板。文章來源:http://www.zghlxwxcb.cn/news/detail-419561.html
PUT /_template/template_1
{
"index_patterns" : ["*"],
"order" : 0,
"settings" : {
"number_of_shards" : 1
},
"version": 123
}
直接查詢版本文章來源地址http://www.zghlxwxcb.cn/news/detail-419561.html
GET /_template/template_1?filter_path=*.version
{
"template_1" : {
"version" : 123
}
}
到了這里,關(guān)于【ES實戰(zhàn)】索引模板template使用說明的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!