目录
一、问题产生背景
在做Elasticsearch和Logstash集成时,由于ES默认的索引分片数是5、副本数是1,导致Logstash在ES中创建索引时,引起ES的健康值变黄。虽然目前看上去没有问题,但是黄颜色,始终不健康。因此,找寻是否有默认索引方案。由此搜索ES官方文档,发现可以添加索引模版(Index template)。
二、作用
当创建索引时,索引模版可以定义索引的设置和相关字段映射。ES会基于匹配索引名字的方式,将模板应用于创建新的索引。
模版索引,只会在索引创建过程中起作用,不会影响已经存在的索引。具体的settings和mapping的配置,可以参考创建索引的api。
如下为原文:
Index templates define settings and mappings that you can automatically apply when creating new indices. Elasticsearch applies templates to new indices based on an index pattern that matches the index name.
Index templates are only applied during index creation. Changes to index templates do not affect existing indices. Settings and mappings specified in create index API requests override any settings or mappings specified in an index template.
三、添加索引模版
请求形式 PUT
内容格式 application/json
请求地址 /_template/<index-template>
<index-template> 索引模版名称
请求body中参数
参数名称 | 是否必传 | 类型 | 默认值 | 说明 |
index_patterns | 是 | array of strings | 在创建过程中,匹配索引名字的通配符 | |
settings | 否 | index setting object | 索引设置,包括分片数和副本数量等,具体详见索引设置 | |
order | 否 | integer | 0 | 该索引模版的权重值。 当有多个索引模版时,order值越小,越先匹配;order值越大,越后匹配 |
create | 否 | boolean | FALSE | 标明该索引模版是否被替换或者更新 |
timeout | 否 | time unit | 30s | 请求响应时间 |
master_timeout | 否 | time unit | 30s | 连接主节点的请求响应时间 |
aliases | 否 | alias object | 索引别名,详见更新索引别名设置 | |
mappings | 否 | mapping object | 索引映射 | |
version | 否 | integer | 索引模版的版本号 |
调用举例
四、更新索引模版
和添加一致,只需要保证索引模版的名字一样就行。
五、查询索引模版
请求形式 GET
内容格式 application/json
请求地址 /_template/<index-template1>[,<index-template2>]
<index-template> 索引模版名称
如果要获取多个索引模版,用逗号隔开
如果不加索引模板名字,则是获取当前ES中所有的索引模版信息。
调用举例
六,删除索引模版
请求形式 DELETE
内容格式 application/json
请求地址 /_template/<index-template>
<index-template> 索引模版名称
调用举例