当需要为同一类索引应用相同的配置、映射、别名时,如果每次创建索引都逐一配置会有些麻烦。索引模板的出现正是为了简化这种操作,使用索引模板你可以方便地为某一类索引自动配置某些共同的参数
- 使用索引模版定制索引结构
假如你想在Elasticsearch中创建两个索引service-log1和service-log2,这两个索引分别记录了不同年份的服务日志数据,它们的映射结构是相同的,也具有相同的分片数和别名。为了实现这一效果,你可以先创建一个索引模板service-template。
PUT _index_template/service-template
{
"index_patterns": [
"service-log*"
],
"template": {
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"serviceid": {
"type": "keyword"
},
"content": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}