【问题标题】:Elastic search get the index template弹性搜索获取索引模板
【发布时间】:2021-06-12 11:45:43
【问题描述】:

我想通过 rest 调用找出索引绑定到哪个模板。所以基本上是通过索引名,得到它属于哪个模板。

基本上,我知道我可以列出所有模板并通过模式查看哪些索引将绑定到模板,但是我们有太多模板和太多排序,很难说。

【问题讨论】:

    标签: elasticsearch elastic-stack


    【解决方案1】:

    您可以为此使用_meta mapping field,以便将任何自定义信息附加到您的索引中。

    假设你有一个这样的索引模板

    PUT _index_template/template_1
    {
      "index_patterns": ["index*"],
      "template": {
        "settings": {
          "number_of_shards": 1
        },
        "mappings": {
          "_meta": {                        <---- add this
            "template": "template_1"        <---- add this
          },                                <---- add this
          "_source": {
            "enabled": true
          },
          "properties": {
            "host_name": {
              "type": "keyword"
            },
            "created_at": {
              "type": "date",
              "format": "EEE MMM dd HH:mm:ss Z yyyy"
            }
          }
        },
        "aliases": {
        }
      },
      "_meta": {
        "description": "my custom template"
      }
    }
    

    一旦您创建并索引匹配该模板的模式,_meta 字段也将使其成为您正在创建的新索引。

    PUT index1
    

    然后,如果您获得新的索引设置,您将看到它是从哪个模板创建的:

    GET index1?filter_path=**._meta.template
    

    =>

    {
      "index1" : {
        "mappings" : {
          "_meta" : {
            "template" : "template_1"            <---- you get this
          },
    

    【讨论】:

    • 老实说,我认为这是天才。让我试一试,然后回复你。
    • 只有一个问题,索引是否可以在创建时从多个模板中获取设置?
    • 酷,很高兴它有帮助。绝对有可能,要么使用index template priority,要么组成不同的component templates
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    相关资源
    最近更新 更多