Elasticsearch: Index template
Index template定义在创建新index时可以自动应用的settings和mappings。 Elasticsearch根据与index名称匹配的index模式将模板应用于新索引。这个对于我们想创建的一系列的Index具有同样的settings及mappings。比如我们希望每一天/月的日志的index都具有同样的设置。

Index template仅在index创建期间应用。 对index template的更改不会影响现有索引。 create index API请求中指定的设置和映射会覆盖索引模板中指定的任何设置或映射。
你可以在代码中加入像C语言那样的block注释。你可以把这个注释放在出来开头 “{”和结尾的“}”之间的任何地方。
定义一个Index template
我们可以使用如下的接口来定义一个index template:
PUT /_template/<index-template>
我们可以使用_template这个终点来创建,删除,查看一个index template。下面,我们来举一个例子:
PUT _template/logs_template
{
"index_patterns": "logs-*",
"order": 1,
"settings": {
"number_of_shards": 4,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"@timestamp": {
"type": "date"
}
}
}
}
在上面,我们可以看到,我们定义了一个叫做logs_template的index template。它的index_patterns定义为“logs-*”,说明,任何以“logs-”为开头的任何一个index将具有在该template里具有的settings及mappings属性。这里的“order”的意思是:如果索引与多个模板匹配,则Elasticsearch应用此模板的顺序。该值为1,表明有最先合并,如果有更高order的template,这个settings或mappings有可能被其它的template所覆盖。
下面,我们来测试一下我们刚定义的index template:
PUT logs-2019-03-01
在这里,我们创建了一个叫做logs-2019-03-01的index。我们使用如下的命令来检查被创建的情况:
GET logs-2019-03-01
显示的结果为:
{
"logs-2019-03-01" : {
"aliases" : { },
"mappings" : {
"properties" : {
"@timestamp" : {
"type" : "date"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1567652671032",
"number_of_shards" : "4",
"number_of_replicas" : "1",
"uuid" : "Dz5rqRS4SEyLM_gf5eEolQ",
"version" : {
"created" : "7030099"
},
"provided_name" : "logs-2019-03-01"
}
}
}
}
证如上所示,我们已经成功创建了一个我们想要的index,并且它具有我们之前定义的settings及mappings。
Index template和alias
我们甚至可以为我们的index template添加index alias:
PUT _template/logs_template
{
"index_patterns": "logs-*",
"order": 1,
"settings": {
"number_of_shards": 4,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"@timestamp": {
"type": "date"
}
}
},
"aliases": {
"{index}-alias" : {}
}
}
在上面,我们已经创立了一个叫做{index}-alias的别名。这里的{index}就是实际生成index的文件名来代替。我们下面用一个例子来说明:
PUT logs-2019-04-01
我们创建一个叫做logs-2019-04-01的index, 那么它同时生成了一个叫做logs-2019-04-01-alias的别名。我们可以通过如下的命令来检查:
GET logs-2019-04-01-alias
显示的结果是:
{
"logs-2019-04-01" : {
"aliases" : {
"logs-2019-04-01-alias" : { }
},
"mappings" : {
"properties" : {
"@timestamp" : {
"type" : "date"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1567653644605",
"number_of_shards" : "4",
"number_of_replicas" : "1",
"uuid" : "iLf-j_G2T4CYcHCqwz32Ng",
"version" : {
"created" : "7030099"
},
"provided_name" : "logs-2019-04-01"
}
}
}
}
Index匹配多个template
多个索引模板可能与索引匹配,在这种情况下,设置和映射都合并到索引的最终配置中。 可以使用order参数控制合并的顺序,首先应用较低的顺序,并且覆盖它们的较高顺序。 例如:
PUT /_template/template_1
{
"index_patterns" : ["*"],
"order" : 0,
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"_source" : { "enabled" : false }
}
}
PUT /_template/template_2
{
"index_patterns" : ["te*"],
"order" : 1,
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"_source" : { "enabled" : true }
}
}
以上的template_1将禁用存储_source,但对于以te *开头的索引,仍将启用_source。 注意,对于映射,合并是“深度”的,这意味着可以在高阶模板上轻松添加/覆盖特定的基于对象/属性的映射,而较低阶模板提供基础。
我们可以来创建一个例子看看:
PUT test10
GET test10
显示的结果是:
{
"test10" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1567654333181",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "iEwaQFl9RAKyTt79PduN-Q",
"version" : {
"created" : "7030099"
},
"provided_name" : "test10"
}
}
}
}
如果我们创建另外一个不是以 “te”开头的index,我们可以看看如下的情况:
PUT my_test_index
GET my_test_index
显示的结果是:
{
"my_test_index" : {
"aliases" : { },
"mappings" : {
"_source" : {
"enabled" : false
}
},
"settings" : {
"index" : {
"creation_date" : "1567654713059",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "aSsIZMT2RyWKT44G2dF2zg",
"version" : {
"created" : "7030099"
},
"provided_name" : "my_test_index"
}
}
}
}
显然在mappings里显示source是被禁止的。
如果对于两个templates来说,如果order是一样的话,我们可能陷于一种不可知论的合并状态。在实际的使用中必须避免。
查询Index template接口
我们可以通过如下的接口来查询已经被创建好的index template:
GET /_template/<index-template>
比如:
GET _template/logs_template
显示的结果是:
{
"logs_template" : {
"order" : 1,
"index_patterns" : [
"logs-*"
],
"settings" : {
"index" : {
"number_of_shards" : "4",
"number_of_replicas" : "1"
}
},
"mappings" : {
"properties" : {
"@timestamp" : {
"type" : "date"
}
}
},
"aliases" : {
"{index}-alias" : { }
}
}
}
显示的内容就是我们之前已经创建的那个index template。
你也可以通过如下的方式来同时查询多个template的情况:
GET /_template/template_1,template_2
GET /_template/temp*
GET /_template
删除一个index template
在之前的练习中,我们匹配“*”,也就是我们以后所有的创建的新的index将不存储source,这个显然不是我们所需要的。我们需要来把这个template进行删除。删除一个template的接口如下:
DELETE /_template/<index-template>
那么针对我们的情况,我们可以使用如下的命令来删除我们不需要的template:
DELETE _template/template_1
DELETE _template/template_2
这样我们删除了我们刚才创建的两个templates。
参考:
【1】https://www.elastic.co/guide/en/elasticsearch/reference/7.4/indices-get-template.html
【2】https://www.elastic.co/guide/en/elasticsearch/reference/7.4/indices-delete-template.html
【3】https://www.elastic.co/guide/en/elasticsearch/reference/7.4/indices-templates.html
Elasticsearch: Index template的更多相关文章
- Elasticsearch之索引模板index template与索引别名index alias
为什么需要索引模板? 在实际工作中针对一批大量数据存储的时候需要使用多个索引库,如果手工指定每个索引库的配置信息(settings和mappings)的话就很麻烦了. 所以,这个时候,就存在创建索引模 ...
- logstash的output配置中指定elasticsearch的template
转自:https://blog.csdn.net/felix_yujing/article/details/78930389 之前采用的是通过filebeat收集nginx的日志,直接到elastic ...
- ElasticSearch Index操作源码分析
ElasticSearch Index操作源码分析 本文记录ElasticSearch创建索引执行源码流程.从执行流程角度看一下创建索引会涉及到哪些服务(比如AllocationService.Mas ...
- elasticsearch模板 template
https://elasticsearch.cn/article/335 elasticsearch模板 template 可以考虑的学习点: mapping的 _default_类型 动态模板:dy ...
- elasticsearch index 之 put mapping
elasticsearch index 之 put mapping mapping机制使得elasticsearch索引数据变的更加灵活,近乎于no schema.mapping可以在建立索引时设 ...
- elasticsearch index 之 create index(二)
创建索引需要创建索引并且更新集群index matedata,这一过程在MetaDataCreateIndexService的createIndex方法中完成.这里会提交一个高优先级,AckedClu ...
- es添加index template
在kibana页面选择最下方的management--elasticsearch--Index Management--Index Management 选择create a template添加in ...
- 索引模板和动态索引模板 (Index Template和Dynamic Template)
相关阅读 Index Templates https://www.elastic.co/guide/en/elasticsearch/reference/7.1/indices-templates.h ...
- Add mappings to an Elasticsearch index in realtime
Changing mapping on existing index is not an easy task. You may find the reason and possible solutio ...
随机推荐
- poj2947(高斯消元法解同余方程组)
题目链接:https://vjudge.net/problem/POJ-2065 题意:题目看着较复杂,实际上就是给了n个同余方程,解n个未知数. 思路:套高斯消元法的模板即可. AC代码: #inc ...
- storm group 的介绍与使用
一.stream group分组介绍 Stream 的分组分为随机分组.字段分组.全部分组.全局分组.无分组.直接分组,自定义分组 二.group的介绍 1.Shuffle grouping:通过tu ...
- HttpServletRequest对象(转)
HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供 ...
- Kubernetes组件-DaemonSet
⒈简介 Replicationcontroller和ReplicaSet都用于在Kubermetes集群上部署运行特定数量的pod.但是,当某些情况下我们希望在集群中的每个节点上运行同一个指定的pod ...
- javaSE总结(一)-java数据类型和运算符
一.注释 (1)单行注释: // (2)多行注释:/* */ (3)文档注释:/** */ 二.标识符和关键字 (1)分隔符:分号; 花括号{} 方括号[] 圆括号() 空格 圆点(.) ...
- 关于springboot的日志logging.file和logging.path的配置问题
springboot日志配置 logging.path logging.file 它们俩不会同时生效,so只配置其中一个就好了. eg1: 单独一个path配置 logging.path=E:/lo ...
- error LNK2001: unresolved external symbol __imp__closesocket@4
环境:Visual C++6.0 问题:链接错误 描述: Linking... NetSrv.obj : error LNK2001: unresolvedexternal symbol __imp_ ...
- DP_Sumsets
Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T ...
- centos7 宝塔php7安装mongodb扩展
一.下载.解压源码 下载地址:https://pecl.php.net/package/mongodb wget -c https://pecl.php.net/get/mongodb-1.5.3.t ...
- S02_CH14_ EMIO_OLED 实验
S02_CH14_ EMIO_OLED 实验 本章将使用EMIO模拟OLED的时序来驱动OLED,本方案对米联系列Miz702,Miz702N和Miz701N全兼容. 14.1板载OLED硬件原理 M ...