ElasticSearch(六):索引模板
## Index Template * Index Template - 幫助你設定`Mappings`和`Settings`,並按照一定的規則,自動匹配到新創建的索引上 - 模板僅在一個索引被創建時,才會產生作用。修改模板不會影響已創建的索引 - 你可以設定多個索引模板,這些設置會被`merge`在一起 - 你可以指定`order`的數值,控制`merging`的過程
#示例一:對所有的索引有效
PUT _template/template_default
{
"index_patterns": ["*"],
"order" : 0,
"version": 1,
"settings": {
"number_of_shards": 1,
"number_of_replicas":1
}
}
#示例二:對以test開頭索引有效
PUT /_template/template_test
{
"index_patterns" : ["test*"],
"order" : 1,
"settings" : {
"number_of_shards": 1,
"number_of_replicas" : 2
},
"mappings" : {
"date_detection": false,
"numeric_detection": true
}
}
#查看template信息
GET /_template/template_default
GET /_template/temp*
#刪除template信息
DELETE /_template/template_default
DELETE /_template/template_test
- 如何使用 Index Template,當一個索引被創建時
- 默認應用Elasticsearch默認的
settings
和mappings
- 然后應用
order
數值低的 IndexTemplate 中的設定 - 再應用
order
數值高的 IndexTemplate 中的設定,之前的設定會被覆蓋 - 最后應用創建索引時,用戶所指定的
settings
和mappings
,並覆蓋之前模板中的設定
- 默認應用Elasticsearch默認的
## Dynamic Template Dynamic Template 是定義在具體索引`mappings`中的,根據Elasticsearch識別的數據類型,結合字段名稱,來動態設定字段類型: * 所有的字符串類型都設定成`keyword`,或者關閉`keyword`字段 * `is`開頭的字段設置成`boolean` * `long_`開頭的都設置成`long`類型
#示例一:Dynaminc Mapping 根據類型和字段名
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"strings_as_boolean": {
"match_mapping_type": "string",
"match":"is*",
"mapping": {
"type": "boolean"
}
}
},
{
"strings_as_keywords": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
}
#示例二:
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"full_name": {
"path_match": "name.*",
"path_unmatch": "*.middle",
"mapping": {
"type": "text",
"copy_to": "full_name"
}
}
}
]
}
}