Contents

elasticsearch的template是很有用,它描述了index的mapping(类似表结构)、setting参数优化,特别是用日期分割index的情况,提前建好template能在自动创建index的时候套用上。一般日志类、自动创建多个同类index等情况推荐在创建index前先定义好。
template可以创建多个,一个template可以对应一组相同结构的index,其定义大概如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
PUT _template/filebeat-bxr-app-pro
{
"index_patterns": ["filebeat-bxr-app-pro-*"],
"settings": {
"number_of_shards": 8,
"number_of_replicas": 1,
"refresh_interval": "120s",
"index.merge.scheduler.max_thread_count": 1,
"index.translog.durability": "async",
"index.translog.sync_interval": "60s",
"index.translog.flush_threshold_size": "1g"
},
"mappings": {
"doc": {
"properties": {
"level": {
"type" : "keyword"
},
"appname": {
"type" : "keyword"
},
"podname": {
"type" : "keyword"
}
}
}
}
}

index_patterns: index的名称,可以用*表示多个index

settings: index的参数,包括定义shard、replica等

  • number_of_replicas: 数据冗余度,1表示1个冗余副本
  • number_of_shards: 分片数,一个index分开多个分片,保存在不同的集群节点,配合routing参数可以更快进行查询操作,类似mysql的分表
  • refresh_interval: 刷新间隔,在 Elasticsearch 中,写入和打开一个新段的轻量的过程叫做 refresh。默认情况下每个分片会每秒自动刷新一次。这就是为什么我们说 Elasticsearch 是近实时搜索: 文档的变化并不是立即对搜索可见,但会在一秒之内变为可见。如果写入数据量大,调整这个参数可以有效降低io压力。
  • merge.scheduler.max_thread_count: 索引 merge 最大线程数,该参数可以有效调节写入的性能。因为在存储介质上并发写,由于寻址的原因,写入性能不会提升,只会降低。
  • translog: 每次 index、bulk、delete、update 完成的时候,一定会触发刷新 translog 到磁盘上,类似mysql binlog
  • translog.durability: async异步写入translog
  • translog.flush_threshold_size: log文件大小

mappings: index中关键字的类型定义,类似mysql的字段属性,也可以设置分词器

  • analyzer: 分词器,默认是英文,也有中文分词器
  • copy_to: 将该字段复制到目标字段,实现类似_all的作用,不会出现在_source中,只用来搜索。列如把字段first_name,last_name,合成full_name做搜索。
  • index: 控制当前字段是否索引,默认为true,即记录索引,false不记录,即不可搜索

更多请参考:
官方文档
Elasticsearch 6.x Mapping设置

Contents