笔记十四:Index Template 和 Dynamic Template
管理多个索引#
- 集群上的索引会越来越多,例如,你会为日志每天创建个索引
- 使用多个索引可以让你的更好的管理的你数据,提高性能
Index Template#
- 使用多个索引可以让你的更好的管理的你数据,提高性能
- Index Templates 帮助你设定 Mapping 和 Settings,并按照一定的规则,自动匹配到新创建的索引之上
- 模板仅在一个索引被新创建时,才会产生作用。修改模板不会影响已创建的索引
- 可以设定多个索引模板,这些设置会被
merge
在一起 - 可以指定
order
的数值,控制merging
的过程
两个 Index Templates#
- index_patterns 索引名称 [“*”] [“test*”] 表示
Index Template 工作方式#
- 当一个索引被新创建时
- 应用
Elasticsearch
默认的settings
和mappings
- 应该
order
数值低的Index Template
中的设定 - 应用
order
高的Index Template
中的设定,之前的设定会被覆盖 - 应用创建索引时,用户所指定的
Settings
和Mappings
,并覆盖之前模板中的设定
- 应用
Demo#
测试默认的索引类型#
#测试下默认的mapping
PUT ttemplate/_doc/1
{
"someNumber" :"1",
"someDate" : "2019/01/01"
}
//发现someDate推算正确,但是someNumber没有推算称number类型
GET ttemplate/_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
}
}
}
}
}
}
}
创建 template#
#创建一个默认的template
PUT _template/template_default
{
"index_patterns" : ["*"],
"order" : 0,
"version": 1,
"settings" :{
"number_of_shards" :1,
"number_of_replicas" :1
}
}
#匹配test开头的patterns
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 开头#
PUT testtemplate/_doc/1
{
"someNumber":"1",
"someDate":"2019/01/01"
}
GET testtemplate/_mapping
{
"testtemplate" : {
"mappings" : {
"date_detection" : false,
"numeric_detection" : true,
"properties" : {
"someDate" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"someNumber" : {
"type" : "long"
}
}
}
}
}
GET testtemplate/_settings
{
"testtemplate" : {
"settings" : {
"index" : {
"creation_date" : "1572275616083",
"number_of_shards" : "1",
"number_of_replicas" : "2",
"uuid" : "s3B3NEaZRDSq586qDVbCqA",
"version" : {
"created" : "7030099"
},
"provided_name" : "testtemplate"
}
}
}
}
设置 test 开头 settings#
PUT testmy
{
"settings":{
"number_of_replicas":5
}
}
PUT testmy/_doc/1
{
"key":"value"
}
//testmy 设置完之后,会覆盖test*开头是settings
GET testmy/_settings
{
"testmy" : {
"settings" : {
"index" : {
"creation_date" : "1572275856520",
"number_of_shards" : "1",
"number_of_replicas" : "5",
"uuid" : "yJJ02g77S6ubgs1w3OVY2g",
"version" : {
"created" : "7030099"
},
"provided_name" : "testmy"
}
}
}
}
Dynamic Template#
- 根据 Elasticsearch 识别的数据类型,结合字段名称,来动态设定字段类型
- 所有的字符串类型都设定称 Keyword,或者关闭 keyword 字段
- is 开头的字段都设置成 boolean
- long_ 开头的都设置成 long 类型
- Dynamic Template 是定义在某个索引的 Mapping 中
- Template 有个名字
- 匹配规则是个一个数组
- 为匹配到字段设置 Mapping
DELETE my_index
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":"Li",
"isVIP":"true"
}
GET my_index/_mapping
//返回部分
"properties" : {
"firstName" : {
"type" : "keyword"
},
"isVIP" : {
"type" : "boolean"
}
}
设置新的 my_index#
DELETE my_index
#结合路径
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"full_name": {
"path_match": "name.*",
"path_unmatch": "*.middle",
"mapping": {
"type": "text",
"copy_to": "full_name"
}
}
}
]
}
}
PUT my_index/_doc/1
{
"name": {
"first": "John",
"middle": "Winston",
"last": "Lennon"
}
}
//可查询到
GET my_index/_search?q=full_name:John
本作品采用《CC 协议》,转载必须注明作者和本文链接