【Elasticsearch】——Index Templates(索引模板)

一、前言

索引模板,简而言之,是一种复用机制。当新建一个 Elasticsearch 索引时,自动匹配模板,完成索引的基础部分搭建。注意:模板只在创建索引时应用。更改模板不会对现有索引产生影响。主要为如下几个部分:

{
  "order": 0,                               // 模板优先级
  "template": "sample_info*",               // 模板匹配的名称方式
  "settings": {...},                        // 索引设置
  "mappings": {...},                        // 索引中各字段的映射定义
  "aliases": {...}                          // 索引的别名
}

二、索引模板样例

{
  "order": 0,
  "template": "sample_info*",
  "settings": {
    "index": {
      "number_of_shards": "64",
      "number_of_replicas": "1"
    }
  },
  "mappings": {
    "info": {
      "dynamic_templates": [
        {
          "string_fields": {
            "mapping": {
              "analyzer": "only_words_analyzer",
              "index": "analyzed",
              "type": "string",
              "fields": {
                "raw": {
                  "ignore_above": 512,
                  "index": "not_analyzed",
                  "type": "string"
                }
              }
            },
            "match_mapping_type": "string",
            "match": "*"
          }
        }
      ],
    "properties": {
        "user_province": {
          "analyzer": "lowercase_analyzer",
          "index": "analyzed",
          "type": "string",
          "fields": {
            "raw": {
              "ignore_above": 512,
              "index": "not_analyzed",
              "type": "string"
            }
          }
        }
      }
    }
  },
  "aliases": {}
}

说明

  • "order"字段:有时候,一个模板可能绝大部分符合新建索引的需求,但是局部需要微调,此时,如果复制旧的模板,修改该模板后,成为一个新的索引模板即可达到我们的需求,但是这操作略显重复。此时,可以采用模板叠加与覆盖来操作。模板的优先级是通过模板中的 order 字段定义的,数字越大,优先级越高。如果存在多个模板,其order相同,那其顺序能保证吗?答案是不能保证,因为对所有模板进行排序的过程中,如果order相同,其顺序无法得到保证。
  • "template" 字段:定义的是该索引模板所应用的索引情况。如 "template": "sample_info*" 所表示的含义是,当新建索引时,所有以 sample_info 开头的索引都会自动匹配到该索引模板。利用该模板进行相应的设置和字段添加等。
  • "settings"字段:索引模板中的 setting 部分一般定义的是索引的主分片、拷贝分片、刷新时间、自定义分析器等。
  • "mappings"字段:主要是一些说明信息,大致又分为_all、_source、prpperties这三部分:
(1) _all:主要指的是AllField字段,我们可以将一个或多个都包含进来,在进行检索时无需指定字段的情况下检索多个字段。设置“_all" : {"enabled" : true}
(2) _source:主要指的是SourceField字段,Source可以理解为ES除了将数据保存在索引文件中,另外还有一份源数据。_source字段在我们进行检索时相当重要,如果在{"enabled" : false}情况下默认检索只会返回ID, 你需要通过Fields字段去到索引中去取数据,效率不是很高。但是enabled设置为true时,索引会比较大,这时可以通过Compress进行压缩和inclueds、excludes来在字段级别上进行一些限制,自定义哪些字段允许存储。
(3) properties:这是最重要的步骤,主要针对索引结构和字段级别上的一些设置。

三、创建模板

PUT _template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_doc": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z yyyy"
        }
      }
    }
  }
}

 四、查看模板

# 已知模板名,查看
GET /_template/template_1

# 通过模糊匹配得到多个模板信息
GET /_template/temp*

# 批量查看模板
GET /_template/template_1,template_2

五、验证模板是否存在

HEAD _template/template_1

六、删除模板

DELETE /_template/template_1

七、索引调优

PUT _template/template_topic
{
  "template": "topic*",
  "order":1,
  "settings": {
    "index":{
      "refresh_interval":"60s",
      "number_of_shards":"3" 
    },
    "translog":{
      "sync_interval":"30s",
      "durability":"async",
      "flush_threshold_size":"1g"
    },
    "merge": {
      "policy.floor_segment": "100mb",
      "policy.max_merged_segment": "2g",
      "scheduler.max_thread_count": "1"
    },
    "number_of_replicas":1
  },
  "mappings": {
    "doc":{
      "dynamic":false,
      "_field_names": {
        "enabled": false
      },
      "properties":{
        "@timestamp": {
          "type": "date"
        },
        "@version": {
          "type": "keyword",
          "norms": false,
          "doc_values":false,
          "index": false
        },
        "message": {
          "type": "text",
          "norms": false,
          "index_options": "docs"
        }
      }
    }
  }
}

说明:

对于索引调优,上述template能够获得较好的索引写入速率。

  • refresh_interval默认为1, 增加到60s。
  • number_of_shards主分片数默认为5,一个主分片的大小为30G比较合适,对于一天有90G的数据,设置为3比较合适。
  • 对于translog的落盘设置,设置为异步,阈值1g,落盘间隔默认为5s, 可增加为30s。
  • 对于merge的设置,segments_per_tier 设置为20 ,可以带来更少的合并,
  • max_merged_segment 默认为5g, 设置为2g,获得更少合并和,减少难度大的合并
  • max_thread_count 当使用HDD时设为1,使用SSD时使用默认值不需要设置
  • floor_segment设置为100mb,减少合并次数

对于mapping的设值,

  • 禁用 dynamic , 禁用_fields_names,
  • 字段关闭doc_value,禁止列存储,写入更快
  • 根据字段属性,来配置:例如,@version, 不需要索引, index设置为false, doc_value设为false, norms设置为false。哪些字段不需要索引,就设置index为false, 不需要评分,norms设置为false,
  • 如果不需要term在哪查到,设置索引等级index_options为docs, 默认offsets 包含id, 偏移量, 频率等信息。 字段设置需要根据业务调整,比如不需要fields子字段,默认text会有keyword子字段

八、参考文章

 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/6.8/indices-templates.html

Index Template 和 Dynamic Template:https://learnku.com/articles/35702

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值