Elasticsearch索引模板_es索引模板(1)

“version”: 3,
“priority”: 100,
“_meta”:{
“description”:“this is order-template”
}
}


        在上面的配置中,index\_patterns用于设置索引模板可以匹配的索引名称,这里使用\*号通配符,表示所有以order开头的索引都会使用上面的模板。这个模板还配置了索引的分片数为3、副本分片数为1、字段映射和别名。priority用来设置模板的优先级,其值越大优先级越高。version表示版本号,\_meta可以保存一些元数据。当模板order-template已经存在时,再次编辑模板配置并发送上述请求可以修改模板的内容。


#### 2.2、查询索引模板


        如果想查询索引模板信息,可以使用下面的代码



GET _index_template/order-template


#### 2.3、创建索引


        索引模板创建之后,当我们创建索引时,如果索引名称被索引模板匹配到,就会自动加载索引模板的配置到索引映射中。


        创建一个索引,索引名为order001



PUT order001


        创建成功后,查看索引的配置信息



GET order001


        查询成功后,可以在代码运行结果中看到模板中的配置已经在索引order001中得到应用,运行结果如下



{
“order001” : {
“aliases” : {
“order” : { }
},
“mappings” : {
“properties” : {
“create_time” : {
“type” : “date”,
“format” : “[yyyy-MM-dd, yyyy-MM-dd HH:mm:ss]”
},
“order_id” : {
“type” : “keyword”
},
“order_name” : {
“type” : “text”,
“fields” : {
“keyword” : {
“type” : “keyword”,
“ignore_above” : 256
}
}
}
}
},
“settings” : {
“index” : {
“creation_date” : “1698127116029”,
“number_of_shards” : “3”,
“number_of_replicas” : “1”,
“uuid” : “gZG1JzzrTtGC4OrvPwujuA”,
“version” : {
“created” : “7090199”
},
“provided_name” : “order001”
}
}
}
}


        如果你想在创建索引时自定义一些配置,可以在创建索引映射时指定,这样就可以覆盖掉索引模板的配置。创建一个索引,名称为order002,自定义分片数为5,副本分片数为2,新增一个字段amount,调整create\_time字段的时间格式,代码如下



PUT order002
{
“settings”: {
“number_of_shards”: 5,
“number_of_replicas”: 2
},
“mappings”: {
“properties”: {
“amount”:{
“type”: “float”
},
“create_time”:{
“type”: “date”,
“format”: [“yyyy/MM/dd HH/mm/ss”]
}
}
}
}


        查看order002的索引映射结果



GET order002


      查询成功后,可以在代码运行结果中看到order002的索引映射中的配置确实成功覆盖了索引模板中的配置



{
“order002” : {
“aliases” : {
“order” : { }
},
“mappings” : {
“properties” : {
“amount” : {
“type” : “float”
},
“create_time” : {
“type” : “date”,
“format” : “[yyyy/MM/dd HH/mm/ss]”
},
“order_id” : {
“type” : “keyword”
},
“order_name” : {
“type” : “text”,
“fields” : {
“keyword” : {
“type” : “keyword”,
“ignore_above” : 256
}
}
}
}
},
“settings” : {
“index” : {
“creation_date” : “1698127721114”,
“number_of_shards” : “5”,
“number_of_replicas” : “2”,
“uuid” : “K3NlLyaGTOW9c8W8ZcSO5g”,
“version” : {
“created” : “7090199”
},
“provided_name” : “order002”
}
}
}
}


#### 2.4、删除索引模板


        删除索引模板,执行如下代码



DELETE _index_template/order-template


#### 2.5、es内置的索引模板


        Elasticsearch内置了两个索引模板,索引模板名称分别为metrics和logs,分别用来匹配名称符合logs-\*-\*和metrics-\*-\*的索引。在创建索引和索引模板时要特别注意不要匹配错索引模板,以避免最后索引配置的效果达不到预期。



GET _index_template

{
“index_templates” : [
{
“name” : “metrics”,
“index_template” : {
“index_patterns” : [
“metrics--
],
“composed_of” : [
“metrics-mappings”,
“metrics-settings”
],
“priority” : 100,
“version” : 0,
“_meta” : {
“managed” : true,
“description” : “default metrics template installed by x-pack”
},
“data_stream” : { }
}
},
{
“name” : “logs”,
“index_template” : {
“index_patterns” : [
“logs--
],
“composed_of” : [
“logs-mappings”,
“logs-settings”
],
“priority” : 100,
“version” : 0,
“_meta” : {
“managed” : true,
“description” : “default logs template installed by x-pack”
},
“data_stream” : { }
}
}
]
}


### 3、模板组件


       按照文章上面的操作我们已经可以快速地创建索引模板,并可以把配置自动应用到匹配的索引中了,但是这样做会使得索引模板的配置内容较多。为了简化索引模板中的配置内容,我们可以把常规的索引设置、映射等内容写成可复用的模板组件,然后在索引模板中引用这些组件,这样模板中的配置内容就会非常简洁,便于移植和管理。


#### 3.1、创建组件模板


        创建组件模板component001和component002



PUT _component_template/component001
{
“template”:{
“mappings”:{
“properties”:{
“date”:{
“type”:“date”,
“format”:[“yyy-MM-dd HH:mm:ss”]
}
}
}
}
}

PUT _component_template/component002
{
“template”: {
“settings”: {
“number_of_shards”: “3”,
“number_of_replicas”: “1”
},
“aliases”: {
“mycomp”: {}
}
}
}


#### 3.2、查看组件模板


        组件模板创建成功后,我们可以执行代码查看组件模板的端点信息



GET _component_template/component*

{
“component_templates” : [
{
“name” : “component002”,
“component_template” : {
“template” : {
“settings” : {
“index” : {
“number_of_shards” : “3”,
“number_of_replicas” : “1”
}
},
“aliases” : {
“mycomp” : { }
}
}
}
},
{
“name” : “component001”,
“component_template” : {
“template” : {
“mappings” : {
“properties” : {
“date” : {
“format” : [
“yyy-MM-dd HH:mm:ss”
],
“type” : “date”
}
}
}
}
}
}
]
}


#### 3.3、使用组件模板


        创建一个索引模板comp-test,把上面两个组件模板加载到索引模板中,索引模板就会匹配所有名称以comp开头的索引。



PUT _index_template/comp-test
{
“index_patterns”: [“comp*”],
“priority”: 100,
“composed_of”: [“component001”, “component002”]
}


        创建索引comp001并查看索引,可以在结果中看到两个组件模板中的配置信息



自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数大数据工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)
img

422)]
[外链图片转存中…(img-E40ASunR-1712934747423)]
[外链图片转存中…(img-jdcmfed3-1712934747423)]
[外链图片转存中…(img-OEJ144fJ-1712934747423)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)
[外链图片转存中…(img-RqTK1pkP-1712934747424)]

  • 25
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值