Elasticsearch之静态模板

一、index Template

这种模板也称之为静态模板,只能控制一些已知名字的字段,提前准备好索引的配置,待使用该类型的索引时,生成与索引模板配置相同的索引提供给用户使用。
Index templates allow you to define templates that will automatically be applied when new indices are created. The templates include both settings and mappings and a simple pattern template that controls whether the template should be applied to the new index.
索引模板允许您定义创建新索引时自动应用的模板。模板包括设置和映射以及一个简单的模式模板,用来控制模板是否应该应用到新的索引中。

Templates are only applied at index creation time. Changing a template will have no impact on existing indices. When using the create index API, the settings/mappings defined as part of the create index call will take precedence over any matching settings/mappings defined in the template.
模板仅在创建索引时应用。更改模板不会对现有索引产生影响。使用 create index API 时,作为创建索引调用一部分定义的设置/映射将优先于模板中定义的任何匹配设置/映射。

二、索引使用场景

由于一个索引已经满足不了业务场景,我们需要按天创建索引存储数据。第一种想法就是创建定时任务,把需要的索引提前创建好。第二种就是利用索引模板本身的机制,如果你插入的数据对应的索引不存在,ES就会去创建一个索引,创建索引时,ES会检查有没有此索引名称匹配的索引模板,如果有就按照此模板创建索引,最后插入数据。

三、模板的增删改查

1. 新增索引模板

PUT _template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_doc": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
}

2. 查询索引模板

  • 获取某个索引模板
GET / _template / template_1
  • 你还可以使用以下通配符匹配多个模板
GET /_template/temp*
GET /_template/template_1,template_2
  • 获取所有索引模板
GET /_template

3. 修改索引模板

在这里插入代码片

4. 删除索引模板

DELETE / _template / template_1

5. 索引模板是否存在

HEAD _template/template_1

四、多模板匹配

索引名称可能和多个索引模板匹配,在这种情况下,设置和映射都合并到索引的最终配置中。合并的顺序可以使用 order 参数来控制,先应用较低的顺序,再覆盖更高的订单(order较高的索引模板和较低的模板取并集,重复的配置取order较高的索引模板)。例如:

PUT /_template/template_1
{
    "index_patterns" : ["*"],
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "_doc" : {
            "_source" : { "enabled" : false }
        }
    }
}

PUT /_template/template_2
{
    "index_patterns" : ["te*"],
    "order" : 1,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "_doc" : {
            "_source" : { "enabled" : true }
        }
    }
}

以上将禁用存储_source,但随着指数的启动te*,_source仍然会被启用。注意,对于映射,合并是“深度”,这意味着可以在高阶模板上轻松添加/覆盖特定的基于对象/属性的映射,而低阶模板提供基础。

使用索引模板的影响

  1. 如果在凌点时,业务仍然很繁忙,此时创建需要的索引必将消耗一些时间,导致数据的堆积。
  2. 其他待补充。。

参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html#versioning-templates

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值