DBAtlas · 数据库运维图谱

用组件模板构建索引模板并模拟结果

Elasticsearch索引生命周期

命令

PUT _component_template/app-mappings
{
  "template": {
    "mappings": {
      "dynamic": "strict",
      "properties": {
        "@timestamp": { "type": "date" },
        "level": { "type": "keyword" },
        "msg": { "type": "text" }
      }
    }
  }
}
PUT _index_template/app-events
{
  "index_patterns": ["app-events-*"],
  "priority": 500,
  "composed_of": ["app-mappings"],
  "template": { "settings": { "number_of_shards": 1, "refresh_interval": "5s" } }
}
# See the final settings and mapping before the index is created:
POST _index_template/_simulate_index/app-events-000001
GET _cat/templates?v&s=name

作用

定义可复用的 mapping 组件模板,将其组合到索引模板中,并在不创建索引的情况下显示将应用于给定索引名的最终 settings 和 mapping。

适用场景

新索引以错误的 mapping 或分片数创建时(动态 mapping 爆炸、用 text 代替 keyword),以及推出模板变更之前。

注意

模板只作用于新创建的索引;现有索引的 mapping 不会改变(参见 reindex-alias-swap)。若两个模板匹配同一模式,只使用优先级最高的那个;它们不会合并。dynamic: strict 会拒绝包含未定义字段的文档;若应用发送新字段,索引会失败。

在 DBAtlas 中打开 →

相关内容