基本思路
创建与索引名关联的templates,写入的时候指定索引名,根据es自动创建索引的特性+templates自动创建自己需要的索引
es动态mapping
dynamic templates
Dynamic templates 用于自定义在动态添加field的时候自动给field设置的数据类型, 给什么类型基于:
- datatype detected by Elasticsearch
- the name or the field
- the full dotted path to the field
创建dynamic templates
Dynamic templates are specified as an array of named objects:
"dynamic_templates": [
{
"my_template_name": {
... match conditions ...
"mapping": { ... }
}
},
...
]
my_template_name
can be any string value- The
match conditions
can include any of : match_mapping_type, match, match_pattern, unmatch, path_match, path_unmatch.- The mapping that the matched field should use
Templates将会顺序执行,直到新增的字段value某一个templates的match condition。可以通过
PUT mapping
API向templates list中追加templates,如果新增的templates和某个旧的重名,它将会替代那个旧的。
templates中的match conditions的类型
match_mapping_type
match_mapping_type用于匹配被dynamic field mapping识别到的es数据类型,换句话说,是es对这个field应该是什么类型的猜测。只有以下数据类型能被es自动识别:
boolean, date, double, long, object, string
可以在定义match_mapping_type的时候用*来表示匹配所有数据类型。
示例:
把自动识别为long类型的field定义为interger类型,把自动识别为string类型的field同时定义为analyzed和not_analyzed:
PUT my_index
{
"mappings": {
"my_type": {
"dynamic_templates": [
{
"integers": {
"match_mapping_type": "long",
"mapping": {
"type": "integer"
}
}
},
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed",
"ignore_above": 256
}
}
}
}
}
]
}
}
}
match and unmatch
match和unmatch定义应用于filedname的pattern。
示例:
定义一个匹配所有以long_开头且不以_text结束的string类型的模板
PUT my_index
{
"mappings": {
"my_type": {
"dynamic_templates": [
{
"longs_as_strings": {
"match_mapping_type": "string",
"match": "long_*",
"unmatch": "*_text",
"mapping": {
"type": "long"
}
}
}
]
}
}
}
match_pattern
用于调整match
参数的行为,比如当match_pattern的值为regex时,match可以支持完整的java正则表达式而不是简单的通配符。
path_match and path_unmatch
用于object类型
{name}和{dynamic_type}
{name}和{dynamic_type}占位符放在mapping中,值为field name和field被es识别的数据类型
覆盖default template
可以通过设置_default_类型的mapping覆盖默认的template,影响范围为所有的indices和types
结合动态mapping和auto create index自动创建索引
- 通过index template api创建索引模板
- 指定索引名写入数据,或者用索引名创建索引
示例如下
(5.x+):
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"
}
}
}
}
}
(2.3)
PUT /_template/template_1
{
"template": "te*",
"settings": {
"number_of_shards": 1
},
"mappings": {
"type1": {
"_source": {
"enabled": false
},
"properties": {
"host_name": {
"type": "string",
"index": "not_analyzed"
},
"created_at": {
"type": "date",
"format": "EEE MMM dd HH:mm:ss Z YYYY"
}
}
}
}
}