Elasticsearch: Index template

时间:2022-07-25
本文章向大家介绍Elasticsearch: Index template,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

腾讯云 Elasticsearch Service】高可用,可伸缩,云端全托管。集成X-Pack高级特性,适用日志分析/企业搜索/BI分析等场景


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_indexGET 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_2GET /_template/temp*GET /_template

删除一个 index template

在之前的练习中,我们匹配 “*”,也就是我们以后所有的创建的新的 index 将不存储 source,这个显然不是我们所需要的。我们需要来把这个 template 进行删除。删除一个 template 的接口如下:

DELETE /_template/<index-template>

那么针对我们的情况,我们可以使用如下的命令来删除我们不需要的 template:

DELETE _template/template_1DELETE _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


最新活动

包含文章发布时段最新活动,前往ES产品介绍页,可查找ES当前活动统一入口

Elasticsearch Service自建迁移特惠政策>>

Elasticsearch Service 新用户特惠狂欢,最低4折首购优惠 >>

Elasticsearch Service 企业首购特惠,助力企业复工复产>>

关注“腾讯云大数据”公众号,技术交流、最新活动、服务专享一站Get~