elasticsearch5.x系列之四(索引模板的使用,详细得不要不要的)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/eases_stone/article/details/82467595

1,首先看一下下面这个索引模板

curl -XPUT "master:9200/_template/template_1?pretty" -H 'Content-Type: application/json' -d'  ---> 模板名字叫做template1
{
    "template" : "hello*",  -------------> 匹配的索引名字
    "order" : 0,       -------------> 代表权重,如果有多个模板的时候,优先进行匹配,值越大,权重越高
    "settings" : {
        "number_of_shards" : 1          -------> 主分片的设置
    },
    "aliases": {
        "alias_1" : {}        ------> 索引对应的别名
    },
    "mappings" : {              -----> 字段的映射
        "_default_": {               -----------------> 默认的配置
            "_source" : { "enabled" : false },     ------> 字段原始的具体值是否要存
            "_all": { "enabled":  false },          -----> 禁用_all 字段,
            "dynamic": "strict"            ----------> 只可以用定义的字段,关闭默认的自动推断数据类型
        },
        "type1" : {                   ----> 类型名字
            "_source" : { "enabled" : true },   ------> 字段具体的值是否要存
            "properties": {           ---------> 字段映射
                "@timestamp": {          -------> 具体的字段映射
                    "type": "date",           
                    "format": "strict_date_optional_time||epoch_millis"
                },
                "@version": {
                    "doc_values": true,
                    "index": "not_analyzed",
                    "type": "string"
                },
                "Guid": {
                    "doc_values": true,
                    "index": "not_analyzed",
                    "type": "string"
                },
                "LogLevel": {
                    "type": "long"
                }
            }
        }
    }
}
'

2,关于索引模板的删除和查看。

删除
curl -X DELETE "localhost:9200/_template/template_1"

查看:
GET /_template/template_1
GET /_template/temp*
GET /_template/template_1,template_2
GET /_template

判断是否存在
HEAD _template/template_1

3,哦了,看完这个简单的模板之后,我们来注意看一下以下几点

注意3.1:不要在一个索引中定义多个type。

6.X版本已经不支持,7.X版本彻底不支持。
扩展问题:5.X版本的父子文档实际实现中是一个索引中定义了多个type,到了6.X中实现方式改变为:join方式。

注意3.2:将Set _source设置为false。

假设你只关心度量结果,不是原始文件内容。
将节省磁盘空间并减少IO。
比如,你可以把原始的数据存储在mysql ,hbase 等其他地方,从es 中得到id 后,去相应的数据库中进行取数据
举例:

“_source”:{
“enabled”:false
},

注意3.3:将_all设置为false。

假设你确切地知道你对哪个field做查询操作?
能实现性能提升,缩减存储。
举例:

“_all”:{
“enabled”:false },

注意3.4:设置dynamic = strict。

假设你的数据是结构化数据。
字段设置严格,避免脏数据注入。
举例:

“dynamic”:”strict”,

注意3.5:使用keyword类型

假设你只关心完全匹配
提高性能和缩小磁盘存储空间
举例:

“CLF_CustomerID”:{
“type”:”keyword”
},

4, 索引模板的用途,一般是用在时间序列这样的索引中

4.1 概述

也就是说,如果你的索引,可以按照每周,或者每天进行建立索引,那么,索引模板的作用就来了。
你只要配置好索引模板,后面你就不用每次都要建立索引的映射了。这个糖果还是很好用的。

4.2 注意的点,索引模板一般配合索引别名一起使用,(下雨天,巧克力和音乐更配哦)

附上官方文档链接,不要太感谢:
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html

猜你喜欢

转载自blog.csdn.net/eases_stone/article/details/82467595