模板类型

模板有两种类型:索引模板组件模板

  1. 组件模板是可重用的构建块,用于配置映射,设置和别名;它们不会直接应用于一组索引。
  2. 索引模板可以包含组件模板的集合,也可以直接指定设置,映射和别名。

    索引模板中的优先级

  3. 可组合模板优先于旧模板。如果没有可组合模板匹配给定索引,则旧版模板可能仍匹配并被应用。

  4. 如果使用显式设置创建索引并且该索引也与索引模板匹配,则创建索引请求中的设置将优先于索引模板及其组件模板中指定的设置。
  5. 如果新数据流或索引与多个索引模板匹配,则使用优先级最高的索引模板。

    内置索引模板

    Elasticsearch具有内置索引模板,每个索引模板的优先级为100,适用于以下索引模式:

  6. logs--

  7. metrics--
  8. synthetics--

所以在涉及内建索引模板时,要避免索引模式冲突。更多可以参考这里

索引模板

  • 索引模板,主要用于在新建索引时自动应用预先设定的配置,简化索引创建的操作步骤
    • 可以设定索引的setting和mapping
    • 可以有多个模板
  • 索引模板API,endpoint为 _template
  1. # 创建索引模板,匹配 test-index-map 开头的索引
  2. PUT _template/template_1
  3. {
  4. "index_patterns": ["test-index-map*"],
  5. "priority" : 1, # 指定优先级, 数值越大优先级越高, 这个模板就越先被应用
  6. "settings": {
  7. "number_of_shards": 1
  8. },
  9. "mappings": {
  10. "doc": {
  11. "_source": {
  12. "enabled": false
  13. },
  14. "properties": {
  15. "name": {
  16. "type": "keyword"
  17. },
  18. "created_at": {
  19. "type": "date",
  20. "format": "YYYY/MM/dd HH:mm:ss"
  21. }
  22. }
  23. }
  24. }
  25. }
  26. # 插入一个文档
  27. POST test-index-map_1/doc
  28. {
  29. "name" : "小旋锋",
  30. "created_at": "2018/08/16 20:11:11"
  31. }
  32. # 获取该索引的信息,可以发现 settings 和 mappings 和索引模板里设置的一样
  33. GET test-index-map_1
  34. # 删除
  35. DELETE /_template/template_1
  36. # 查询
  37. GET /_template/template_1
参数 参数类型 是否必传 描述
index_patterns 字符串数组 通配符表达式数组,用于在创建过程中匹配数据流和索引的名称。
data_stream 对象 指示模板是否用于创建数据流及其支持索引
template 对象 应用的模板。它可任选地包括一个aliases,mappings或 settings配置,这些参数和创建索引的时候配置的一样
composed_of 字符串数组 组件模板名称的有序数组。组件模板按照指定的顺序合并,这意味着指定的最后一个组件模板具有最高优先级
priority 整数 确定索引模板优先级的优先级,如果未指定优先级,则将模板视为优先级为0(最低优先级)
version 整数 用于从外部管理索引模板的版本号,用户自定义
_meta 对象 有关索引模板的可选用户元数据,用户自定义

获取所有索引模板

GET http://172.16.1.236:9201/_index_template

组件模板

  • 首先创建两个索引组件模板: ```java PUT _component_template/component_template1 { “template”: { “mappings”: {
    "properties": {
      "@timestamp": {
        "type": "date"
      }
    }
    
    } } }

PUT _component_template/runtime_component_template { “template”: { “mappings”: { “runtime”: { “day_of_week”: { “type”: “keyword”, “script”: { “source”: “emit(doc[‘@timestamp’].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))” } } } } } }

执行结果如下<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/520075/1654894060953-94960deb-c882-49dc-bafa-6fc27fd42129.png#clientId=uf329560b-8487-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=295&id=u017a6726&margin=%5Bobject%20Object%5D&name=image.png&originHeight=589&originWidth=690&originalType=binary&ratio=1&rotation=0&showTitle=false&size=197702&status=done&style=none&taskId=udba5c863-2914-425b-88a3-dba39e4e97b&title=&width=345)

- **创建使用组件模板的索引模板**
```java
PUT _index_template/template_1
{
  "index_patterns": ["bar*"],
  "template": {
    "settings": {
      "number_of_shards": 1
    },
    "mappings": {
      "_source": {
        "enabled": true
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z yyyy"
        }
      }
    },
    "aliases": {
      "mydata": { }
    }
  },
  "priority": 500,
  "composed_of": ["component_template1", "runtime_component_template"], 
  "version": 3,
  "_meta": {
    "description": "my custom"
  }
}
  • 创建一个匹配bar*的索引bar-test ```java PUT /bar-test
然后获取mapping
```java
GET /bar-test/_mapping

image.png

模拟某个索引结果

比如上面的template_1, 我们不用创建bar*的索引(这里模拟bar-pdai-test),也可以模拟计算出索引的配置:

POST /_index_template/_simulate_index/bar-pdai-test

image.png

模拟组件模板结果

当然,由于template_1模板是由两个组件模板组合的,我们也可以模拟出template_1被组合后的索引配置:
POST /_index_template/_simulate/template_1
执行结果如下:

{
  "template" : {
    "settings" : {
      "index" : {
        "number_of_shards" : "1"
      }
    },
    "mappings" : {
      "runtime" : {
        "day_of_week" : {
          "type" : "keyword",
          "script" : {
            "source" : "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))",
            "lang" : "painless"
          }
        }
      },
      "properties" : {
        "@timestamp" : {
          "type" : "date"
        },
        "created_at" : {
          "type" : "date",
          "format" : "EEE MMM dd HH:mm:ss Z yyyy"
        },
        "host_name" : {
          "type" : "keyword"
        }
      }
    },
    "aliases" : {
      "mydata" : { }
    }
  },
  "overlapping" : [ ]
}

模拟组件模板和自身模板结合后的结果

  • 新建两个模板 ```java PUT /_component_template/ct1 { “template”: { “settings”: {
    "index.number_of_shards": 2
    
    } } }

PUT /_component_template/ct2 { “template”: { “settings”: { “index.number_of_replicas”: 0 }, “mappings”: { “properties”: { “@timestamp”: { “type”: “date” } } } } }

模拟在两个组件模板的基础上,添加自身模板的配置
```java
POST /_index_template/_simulate
{
  "index_patterns": ["my*"],
  "template": {
    "settings" : {
        "index.number_of_shards" : 3
    }
  },
  "composed_of": ["ct1", "ct2"]
}

执行的结果如下

{
  "template" : {
    "settings" : {
      "index" : {
        "number_of_shards" : "3",
        "number_of_replicas" : "0"
      }
    },
    "mappings" : {
      "properties" : {
        "@timestamp" : {
          "type" : "date"
        }
      }
    },
    "aliases" : { }
  },
  "overlapping" : [ ]
}