IndexTemplate索引模板

我们可以在es中内置默认的通用模板,创建索引时根据一定的顺序使用相应的模板应用在索引中,避免了大量重复创建模板

1
2
3
4
5
6
7
8
9
10
11
12
13
PUT _template/template_default
{
"index_patterns": ["*"],
"order": 0,
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"date_detection": true, //时间类型探测
"numeric_detection": true //数字类型探测
}
}

程序优先使用创建索引时的设置,没有则使用order数值高的设定,若也没有则使用es内置的默认设定

2 DanamicTemplate 动态模板

根据es识别的数据类型, 结合字段名称动态设定字段类型

eg

将所有字符串类型设定成keyword

is 开头的设置为boolean

long_ 开头的设置为long

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
PUT users
{
"mappings": {
"dynamic_templates": [
{
"full_name": {
"path_match": "name.*",
"path_unmatch": "*.middle",
"mapping": {
"type": "text",
"copy_to": "full_name"
}
}
},{
"string_as_boolean": {
"match_mapping_type": "string",
"match": "is*",
"mapping":{
"type": "boolean"
}
}
}
]
}
}