1. 什么是 Index Template?
Index Template帮你设定 mappings 和 Settings ,并按照一定规则,自动匹配到新创建的索引值上
注意:
模版仅在一个索引被创建时,才会产生作用,修改模版不会影响已经创建的索引
你可以设定多个索引模版,这些设置会被 “merge” 在一起
你可以指定“order”的数值,控制“meriging”的过程
2. Index Template的工作方式:
当一个索引被创建时,
应用 ElasticSearch 默认的 Settings 和 mappings,
应用 order 数值低的 Index Template 中的设定,
应用 order 高的 Index Template 中的设定, 之前的设定会被覆盖,
应用创建索引时, 用户所指定的 Setting 和 mappings , 并覆盖之前模板中的设定
创建模版:
// 创建一个默认的模版
PUT _template/template_default
{
"index_patterns" : ["*"], //指定索引模式(*表示所有)
"order" : 0, //控制合并的顺序,先应用较低的顺序,然后使用较高的顺序覆盖它们
"version" : 1, //版本
"settings" : {
"number_of_shards" : 1, //主分片数,默认为5.只能在创建索引时设置,不能修改
"number_of_replicas" : 1 //每个主分片的副本数。默认为 1。
},
"aliases": {
"charger-query": {} //起别名
}
}
返回:
{
"acknowledged": true //创建成功
}
// 创建一个test开头的模版
PUT _template/template_test
{
"index_patterns": ["test*"],
"order" : 1,
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 2
},
"mapping":{
"date_detection":false, //设置字符串识别日期功能关闭
"numeric_detection":true //设置字符串识别为数字功能打开
}
}
返回:
{
"acknowledged": true
}
// 查看默认template 信息
GET _template/template_default
返回:
{
"template_default": {
"order": 0,
"version": 1,
"index_patterns": [
"*"
],
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "1"
}
},
"mappings": {},//映射
"aliases": { //别名
"charger-query": {}
}
}
}
//通过通配符*查询索引模版
GET _template/template_*
返回:
{
"template_default": {
"order": 0,
"version": 1,
"index_patterns": [
"*"
],
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "1"
}
},
"mappings": {},
"aliases": {
"charger-query": {}
}
},
"template_test": {
"order": 1,
"index_patterns": [
"test*"
],
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "2"
}
},
"mappings": {},
"aliases": {}
},
"template_default1": {
"order": 0,
"version": 1,
"index_patterns": [
"*"
],
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "1"
}
},
"mappings": {},
"aliases": {}
}
}
//新增文档指定id
PUT test_template/_doc/2
{
"someNumber" : "1",
"someDate" : "2020/03/12 23:27:45"
}
返回:
{
"_index": "test_template",//索引名称
"_type": "_doc",//文档类型
"_id": "2",//文档id
"_version": 1,//版本号
"result": "created",//状态码
"_shards": {
"total": 3,//分片数量
"successful": 1, //成功一条
"failed": 0 //失败0条
},
"_seq_no": 1,//控制并发的
"_primary_term": 1 //用来恢复数据时处理当多个文档的_seq_no一样时的冲突,避免Primary Shard上的写入被覆盖
}
// 获取setting
GET test_template/_settings
返回:
{
"test_template": {
"settings": {
"index": {
"creation_date": "1584026827267",//创建时间
"number_of_shards": "1",//主分片数,默认为5.只能在创建索引时设置,不能修改
"number_of_replicas": "2",//每个主分片的副本数。默认为 1。
"uuid": "XdlqWKlLQ5K-vylX4o-iJA",
"version": {
"created": "6020299"
},
"provided_name": "test_template"
}
}
}
}
什么是 Dynamic Template?
- 根据 ElasticSearch 识别的数据类型, 结合字段名称, 来动态设定字段类型
- Dynamic Template 是定义在某个索引的Mapping 中
- 匹配规则是一个数组
- 为匹配到字段设置 Mapping
PUT my_index/_doc/1
{
"firstName" : "charger",
"isVip" : "true",
"createTime":"2020-09-09 09:09:20",
"count":1,
"number":"1"
}
GET my_index/_mapping
返回:
{
"my_index": {
"mappings": {
"_doc": {
"properties": {
"count": {
"type": "long"
},
"createTime": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"firstName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"isVip": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"number": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
- text类型:会分词,先把对象进行分词处理,然后再再存入到es中。
当使用多个单词进行查询的时候,当然查不到已经分词过的内容! - keyword:不分词,没有把es中的对象进行分词处理,而是存入了整个对象!
这时候当然可以进行完整地查询!默认是256个字符