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"
}
}
}
]
}
}