什么是 Index Template
- Index Templates - 帮你设定 mappings 和 Settings ,并按照一定规则,自动匹配到新创建的索引值上
- 模版仅在一个索引被创建时,才会产生作用,修改模版不会影响已经创建的索引
- 你可以设定多个索引模版,这些设置会被 “merge” 在一起
- 你可以指定“order”的数值,控制“meriging”的过程
Index Template 的工作方式
- 当一个索引被创建时
- 应用 ElasticSearch 默认的 Settings 和 mappings
- 应用 order 数值低的 Index Template 中的设定
- 应用 order 高的 Index Template 中的设定, 之前的设定会被覆盖
- 应用创建索引时, 用户所指定的 Setting 和 mappings , 并覆盖之前模板中的设定
创建一个默认的模版
// 创建一个默认的模版
PUT _template/template_default
{
"index_patterns" : ["*"], // 通配符表示所有的 模版
"order" : 0,
"version" : 1,
"settings" : {
"number_of_shards" : 1, // 分片1
"number_of_replicas" : 1 // 备份1
}
}
// 返回结果
{
"acknowledged" : true // 创建成功
}
// 创建一个test 开头的模版
PUT _template/template_test
{
"index_patterns" : ["test*"],
"order" : 1,
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 2
},
"mappings" : {
"date_detection" : false, // 设置字符串识别日期功能关闭
"numeric_detection" : true // 设置字符串识别为数字功能打开
}
}
// 查看template 信息
GET _template/template_default
// 返回信息
{
"template_default" : {
"order" : 0,
"version" : 1,
"index_patterns" : [
"*"
],
"settings" : {
"index" : {
"number_of_shards" : "1",
"number_of_replicas" : "1"
}
},
"mappings" : { },
"aliases" : { }
}
}
// 通过通配符
GET _template/template_*
// 返回信息
{
"template_test" : {
"order" : 1,
"index_patterns" : [
"test*"
],
"settings" : {
"index" : {
"number_of_shards" : "1",
"number_of_replicas" : "2"
}
},
"mappings" : {
"numeric_detection" : true,
"date_detection" : false
},
"aliases" : { }
},
"template_default" : {
"order" : 0,
"version" : 1,
"index_patterns" : [
"*"
],
"settings" : {
"index" : {
"number_of_shards" : "1",
"number_of_replicas" : "1"
}
},
"mappings" : { },
"aliases" : { }
}
}
测试 test 开头的 template
PUT testtemplate/_doc/1
{
"someNumber" : "1",
"someDate" : "2019/01/01"
}
// 获取mapping 查看
{
"testtemplate" : {
"mappings" : {
"date_detection" : false,
"numeric_detection" : true,
"properties" : {
"someDate" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"someNumber" : {
"type" : "long"
}
}
}
}
}
// 获取setting
GET testtemplate/_settings
{
"testtemplate" : {
"settings" : {
"index" : {
"creation_date" : "1563174004803",
"number_of_shards" : "1",
"number_of_replicas" : "2",
"uuid" : "kPDbWBwWSPaoroTWfx8WoA",
"version" : {
"created" : "7010099"
},
"provided_name" : "testtemplate"
}
}
}
}
如果是在创建索引的时候,指定了setting,他就不会去选择 template 了 。
感谢评论的大佬指正的错误。
其实是创建索引的时候,制定了某些设置,那么索引便会优先使用这些设置。其他设置还是会去选择 template 中设置。谢谢评论的大佬!!!
什么是 Dynamic Template
- 根据 ElasticSearch 识别的数据类型, 结合字段名称, 来动态设定字段类型
- 所有的字符串类型都设定成 Keyword,或者关闭 Keyword 字段
- is 开头的字段都设置成 boolean
- long_ 开头的都设置为 long 类型
- Dynamic Template 是定义在某个索引的Mapping 中
- Template 有一个名称
- 匹配规则是一个数组
- 为匹配到字段设置 Mapping
示例
//首先添加一个 index 查看mapping
PUT my_index/_doc/1
{
"firstName" : "Gao",
"isVip" : "true"
}
GET my_index/_mapping
{
"my_index" : {
"mappings" : {
"properties" : {
"firstName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"isVip" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
// 再为该index 设置 dynamic template