1.索引模版
当需要为同一类索引应用相同的配置,映射,别名时,如果每次创建索引都逐一配置繁琐。
索引模版简化这种配置。
2.使用索引模版定制索引结构
假设有两个索引,除了名称不同,结构相同。可以引用相同的模版。
数据不同
结构相同
分片数相同
别名也相同
#创建索引模版 service-template
curl -u elastic:elastic -k -XPUT "http://192.168.1.800:9200/_index_template/service-template" -H 'Content-Type: application/json' -d '
{
"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}}},
"created_at":{"type":"date","format":"yyyy-MM-dd HH:mm:ss"}
}
},
"aliases":{"service-logs":{}}
},
"priority":200,
"version":3,
"_meta":{"description":"存储大数据"}
}'
"index_patterns":用于设置索引模版可以匹配的索引名称前缀。
该模版配置了分片数,副本数,字段名,别名。
"priority":用来设置模版的优先级,值越大,优先级越高。
"version":表示版本号。
"_meta":可以保存一些元数据。
当模版已经存在时,发送上面的命令可以修改模版的内容。
#查看索引模版
curl -u elastic:elastic -k -XGET "http://192.168.1.800:9200/_index_template/service-template"
{
"index_templates":[
{
"name":"service-template",
"index_template":{
"index_patterns":[
"service-log*"
],
"template":{
"settings":{
"index":{
"number_of_shards":"5",
"number_of_replicas":"1"
}
},
"mappings":{
"properties":{
"created_at":{
"format":"yyyy-MM-dd HH:mm:ss",
"type":"date"
},
"serviceid":{
"type":"keyword"
},
"content":{
"type":"text",
"fields":{
"keyword":{
"ignore_above":256,
"type":"keyword"
}
}
}
}
},
"aliases":{
"service-logs":{
}
}
},
"composed_of":[
],
"priority":200,
"version":3,
"_meta":{
"description":"存储大数据"
}
}
}
]
}
#创建索引:
curl -u elastic:elastic -k -XPUT "http://192.168.1.800:9200/service-log1"
curl -u elastic:elastic -k -XGET "http://192.168.1.800:9200/service-log1"
{
"service-log1":{
"aliases":{
"service-logs":{
}
},
"mappings":{
"properties":{
"content":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
},
"created_at":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss"
},
"serviceid":{
"type":"keyword"
}
}
},
"settings":{
"index":{
"routing":{
"allocation":{
"include":{
"_tier_preference":"data_content"
}
}
},
"number_of_shards":"5",
"provided_name":"service-log1",
"creation_date":"1743659688500",
"number_of_replicas":"1",
"uuid":"iEf0mKAuQKCrb311hy0dhw",
"version":{
"created":"7170099"
}
}
}
}
}
3.覆盖索引模版
如果想在索引 service-log2中自定义某些内容,可以在创建索引映射时指明,
这样就可以覆盖掉索引模版。
curl -u elastic:elastic -k -XDELETE "http://192.168.1.800:9200/service-log2"
curl -u elastic:elastic -k -XPUT "http://192.168.1.800:9200/service-log2" -H 'Content-Type: application/json' -d '
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"properties":{
"level":{"type":"text"},
"serviceid":{"type":"long"}
}
}
}'
#字段名称相同的字段会用自定义的覆盖模版,字段名称和模版不同的会和模版做合并。
#既能继承模版的内容,又能重写模版的内容。
curl -u elastic:elastic -k -XGET "http://192.168.1.800:9200/service-log2"
{
"service-log2":{
"aliases":{
"service-logs":{
}
},
"mappings":{
"properties":{
"content":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
},
"created_at":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss"
},
"level":{
"type":"text"
},
"serviceid":{
"type":"long"
}
}
},
"settings":{
"index":{
"routing":{
"allocation":{
"include":{
"_tier_preference":"data_content"
}
}
},
"number_of_shards":"3",
"provided_name":"service-log2",
"creation_date":"1743660034061",
"number_of_replicas":"1",
"uuid":"JHGcjPquQKuYfKlb0_9I_A",
"version":{
"created":"7170099"
}
}
}
}
}
4.注意内置模版
ES内置了两个索引模版分别匹配名称符合 logs-*-* 和 metrics-*-* 的索引。
所以创建索引时,不要使用和内置模版名称相同的索引名称。