赞
踩
Index Template
和
Dynamic Template
来更好的为我们管理和设置索引和mapping。
比如一个我们需要使用ES来做日志管理,我们都知道日志的数据量是十分庞大的,如果使用单个索引来保存所有日志数据的话,可能会存在一些性能问题。我们可以通过按天或月来自动生成index,这时候我们就可以用到IndexTemplate,可以为索引和ES集群提供更好的性能。
Index Template:可以帮助你设定Mapping和Setting,并按照一定的规则,自动匹配到新创建的索引上。
order
的数值,来控制合并的过程。当一个索引被创建时,所引用的template规则如下:
比如,我们现在来创建又给默认模板,如果所创建的索引没有指定分片的话,就以当前模板配置为准。
put _template/template_default
{
"index_patterns":["*"],
"order":0,
"version":1,
"settings":{
"number_of_shards":1,
"number_of_replicas":1
}
}
然后我们不指定分片信息来创建一个索引,并获取索引的信息。
put template_index
--------------------
get template_index
--------------------
{
"template_index" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "template_index",
"creation_date" : "1640524317827",
"number_of_replicas" : "1",
"uuid" : "e5zbL0_lQUyYWFkDqmx5xw",
"version" : {
"created" : "7160099"
}
}
}
}
}
可以看到索引已经按照我们设定的默认模板,来自动生成了分片的配置。
再创建一个模板,匹配规则为:索引的名称以template开头的,设定分片数量为1,副本数量为3。
date_detection
:mapping设置,如果字符串符合日期的类型,就自动匹配为日期,true为开启,false为关闭
numeric_detection
:mapping设置,如果字符串是数字的话,自动映射数字类型。
put _template/template_test
{
"index_patterns":["template*"],
"order":1,
"settings":{
"number_of_shards":1,
"number_of_replicas":3
},
"mappings":{
"date_detection":false,
"numeric_detection":true
}
}
我们创建一个匹配通配规则的索引,获取信息。可以看到如下,匹配到了template_test
,因为template_default
的order为0,所以以template_test
的分片副本设置为准。
put template_index2
---------------------
get template_index2
---------------------
{
"template_index2" : {
"aliases" : { },
"mappings" : {
"date_detection" : false,
"numeric_detection" : true
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "template_index2",
"creation_date" : "1640524532090",
"number_of_replicas" : "3",
"uuid" : "QsP_eZuhQKWiaGvF_6ux4A",
"version" : {
"created" : "7160099"
}
}
}
}
}
我们来插入一个文档,再获取mapping,看看ES是否自动将日期和数字做了转换。
put template_index2/_doc/1
{
"date":"2021-12-01T00:00:00.000Z",
"num":"10000"
}
-----------------------------------
{
"template_index2" : {
"mappings" : {
"date_detection" : false,
"numeric_detection" : true,
"properties" : {
"date" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"num" : {
"type" : "long"
}
}
}
}
}
可以看到,由于我们设置的 "date_detection":false,"numeric_detection":true
。所以就算匹配到了date类型也会给到text的字段类型,数值类型被匹配到了long类型。
我们再创建一个匹配模板的索引,指定setting的分片和副本数量,看看是否会以我们设定的为准。
put template_index3
{
"settings":{
"number_of_shards":2,
"number_of_replicas":2
}
}
-----------------
{
"template_index3" : {
"aliases" : { },
"mappings" : {
"date_detection" : false,
"numeric_detection" : true
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "2",
"provided_name" : "template_index3",
"creation_date" : "1640524899092",
"number_of_replicas" : "2",
"uuid" : "nW7wNjmmQaqjgasWG8_7zQ",
"version" : {
"created" : "7160099"
}
}
}
}
}
由索引的mapping信息可以看到,当前这个索引时匹配到了template_test
这个模板的,但是由于我们在创建索引的时候指定了分片和副本的信息,所以以我们指定的为准。
上面我们讲了Index Template,主要是在创建索引的时候,来根据template的统配规则,确定当前创建的索引是否符合,从而将template里面的配置信息应用在我们新创建的索引中,通常应用于要生成相同配置的索引场景下,比如日志数据管理、统一索引管理等。
Dynamic Template主要是应用于具体的索引中去的,定义在某个索引的mapping设置中,会根据我们设定的数据类型,匹配一些设定的规则,来动态设定字段类型。
比如:字符串全匹配Keyword、is开头的字段设置为boolean,long_开头的字段设置为long等等…都可以由我们来设置模板动态匹配,为匹配到的字段自定设置我们自定义的字段类型。
比如我们创建一个索引,需要将索引中的字段name中的子字段给copy_to到一个新字段full_name
以供查询,但是name中的address属性是不需要的。
put template_mapping
{
"mappings":{
"dynamic_templates":[
{
"full_name":{
"path_match":"name.*",
"path_unmatch":"*.address",
"mapping":{
"type":"text",
"copy_to":"full_name"
}
}
}
]
}
}
然后我们插入一个文档
put template_mapping/_doc/1
{
"name":{
"first":"程",
"middle":"大",
"last":"帅",
"address":"上海市汤臣一品"
}
}
获取下ES根据我们设定的Dynamic Template自动生成的mapping。
get template_mapping/_mapping
---------------------------
{
"template_mapping" : {
"mappings" : {
"dynamic_templates" : [
{
"full_name" : {
"path_match" : "name.*",
"path_unmatch" : "*.address",
"mapping" : {
"copy_to" : "full_name",
"type" : "text"
}
}
}
],
"date_detection" : false,
"numeric_detection" : true,
"properties" : {
"full_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"properties" : {
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"first" : {
"type" : "text",
"copy_to" : [
"full_name"
]
},
"last" : {
"type" : "text",
"copy_to" : [
"full_name"
]
},
"middle" : {
"type" : "text",
"copy_to" : [
"full_name"
]
}
}
}
}
}
}
}
同时因为fulfill_name是由name.*中的数据copy_to而来的,所以我们可以对其进行搜索,但是在文档的_srouce中,不存在这个字段。
get template_mapping/_search
{
"query":{
"match":{
"full_name": "程"
}
}
}
------------------------
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "template_mapping",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : {
"first" : "程",
"middle" : "大",
"last" : "帅",
"address" : "上海市汤臣一品"
}
}
}
]
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。