将索引模板与动态模板组合在一起

时间:2016-07-29 13:58:03

标签: elasticsearch

我想将Index TemplatesDynamic Templates结合起来,以便我定义的动态映射自动添加到所有创建的索引中。

这可能吗?

关心莫滕

3 个答案:

答案 0 :(得分:3)

在索引模板中将映射定义为动态模板。 例如:

.py

答案 1 :(得分:1)

您可以这样做:

PUT /_template/my_template
{
  "template": "name-*",
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "rule1": {
            "match": "field*",
            "mapping": {
              "type": "string",
              "index": "analyzed"
            }
          }
        },
        {
          "rule2": {
            "match": "another*",
            "mapping": {
              "type": "integer"
            }
          }
        }
      ],
      "properties": {
         "field": {
             "type": "string"
         }
      }
    }
  }
}

答案 2 :(得分:1)

在Elasticsearch 7.x中,文档类型现在为deprecated。因此,旧答案中的示例将引发异常。解决方案如下:

PUT /_template/test_template
{
  "index_patterns" : [
    "test*"
  ],
  "mappings": {
    "dynamic_templates": [
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "text",
            "fields": {
              "keyword": {
                "type":  "keyword",
                "ignore_above": 1024
              }
            }
          }
        }
      }
    ]
  }
}

P.S。另外,我建议将索引类型“ _doc”设置到旧的应用程序中,以便现在与这些模板兼容(例如,fluent-bit v1.3)。