Index Template&Dynamic Template
# 一、Index Template
# 1、概念
Index Templates帮助我们设定Mappings和Settings,并按照一定的规则自动匹配到新创建的索引上。
- 模板仅在一个索引被新创建时,才会产生作用。修改模板不会影响已创建的索引。
- 可以设定多个模板,这些设置会merge在一起。
- 可以指定order的数值,控制merge的过程。
# 2、工作方式
当一个索引创建的时候:
- 应用Elasticsearch默认的settings和mappings。
- 应用order数值低的Index Template的设定。
- 应用order数值高的Index Template的设定,之前的设定会被覆盖掉。
- 应用创建索引的时候,如果用户指定了Settings和Mappings,就会覆盖之前模板的设定。
# 3、案例实践
ES默认的settings和mappings
// 数字字符串被映射成text,日期字符串被映射成日期,因为是使用ES默认的settings和mappings
PUT ttemplate/_doc/1
{
"someNumber":"1",
"someDate":"2019/01/01"
}
GET ttemplate/_mapping
GET ttemplate/_settings
// mapping
{
"ttemplate" : {
"mappings" : {
"properties" : {
"someDate" : {
"type" : "date",
"format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
},
"someNumber" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
// settings
{
"ttemplate" : {
"settings" : {
"index" : {
"creation_date" : "1684892721593",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "rgNZ2YLCSO2hfr1mqU41qg",
"version" : {
"created" : "7010099"
},
"provided_name" : "ttemplate"
}
}
}
}
创建两个Index Template
#Create a default template
PUT _template/template_default
{
"index_patterns": ["*"],
"order" : 0,
"version": 1,
"settings": {
"number_of_shards": 1,
"number_of_replicas":1
}
}
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
GET /_template/temp*
插入index以test开头
#写入新的数据,index以test开头
PUT testtemplate/_doc/1
{
"someNumber":"1",
"someDate":"2019/01/01"
}
GET testtemplate/_mapping
GET testtemplate/_settings
// mapping
{
"testtemplate" : {
"mappings" : {
"date_detection" : false,
"numeric_detection" : true,
"properties" : {
"someDate" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"someNumber" : {
"type" : "long"
}
}
}
}
}
// settings
{
"testtemplate" : {
"settings" : {
"index" : {
"creation_date" : "1684893022481",
"number_of_shards" : "1",
"number_of_replicas" : "2",
"uuid" : "ZpqS8MJhStabGV5SuIjl7w",
"version" : {
"created" : "7010099"
},
"provided_name" : "testtemplate"
}
}
}
}
插入自我配置的数据
PUT testmy
{
"settings":{
"number_of_replicas":5
}
}
put testmy/_doc/1
{
"key":"value"
}
get testmy/_settings
{
"testmy" : {
"settings" : {
"index" : {
"creation_date" : "1684893154540",
"number_of_shards" : "1",
"number_of_replicas" : "5",
"uuid" : "6DgFXdp6S8-XwVuqAQV-PQ",
"version" : {
"created" : "7010099"
},
"provided_name" : "testmy"
}
}
}
}
# 二、Dynamic Template
# 1、概念
由于Dynamic Mapping会自动根据自动根据文档信息,推算出字段的类型,我们可以设定Dynamic Template来设定Elasticsearch识别的数据类型,结合字段名称,来动态设定字段的类型:
- is开头的都设置为boolean。
- long_开头的都设置为long类型。
- 所有的字符串类型都设定成keyword。
# 2、案例实践
Dynamic Mapping自动推断
DELETE my_index
PUT my_index/_doc/1
{
"firstName":"Ruan",
"isVIP":"true"
}
GET my_index/_mapping
DELETE my_index
{
"my_index" : {
"mappings" : {
"properties" : {
"firstName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"isVIP" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
设定Dynamic Template
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"strings_as_boolean": {
"match_mapping_type": "string",
"match":"is*",
"mapping": {
"type": "boolean"
}
}
},
{
"strings_as_keywords": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
}
PUT my_index/_doc/1
{
"firstName":"Ruan",
"isVIP":"true"
}
GET my_index/_mapping
{
"my_index" : {
"mappings" : {
"dynamic_templates" : [
{
"strings_as_boolean" : {
"match" : "is*",
"match_mapping_type" : "string",
"mapping" : {
"type" : "boolean"
}
}
},
{
"strings_as_keywords" : {
"match_mapping_type" : "string",
"mapping" : {
"type" : "keyword"
}
}
}
],
"properties" : {
"firstName" : {
"type" : "keyword"
},
"isVIP" : {
"type" : "boolean"
}
}
}
}
}
# 参考
上次更新: 2024/06/29, 15:13:44