elasticsearch模板
因工作中使用到es非关系型数据库,在新建立索引时,涉及到自定义字段类型,因此整理学习demo。使用模板文件建立索引,自动映射mapping,本次采用最新elasticsearch数据库:es8
一、通过字段类型匹配
1.新增模板文件
请求:PUT http://localhost:9200/_template/my_template_test (my_template_test->模板名称)
{
"index_patterns": [
"lt*"
], // 可以通过"lt*来适配, template字段已过期
"order": 0, // 模板的权重, 多个模板的时候优先匹配用, 值越大, 权重越高
"settings": {
"number_of_shards": 1, // 分片数量, 可以定义其他配置项
"number_of_replicas": "1", // 副本数
"refresh_interval": "5s" // 刷新时间
},
"aliases": {
"lt_test": {} // 索引对应的别名
},
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
},//固定映射
"dynamic_templates": [
{
"handleLong": { 动态模板名称
"match_mapping_type": "long",//通过类型映射 es数据中数字默认long类型,long类型映射为integer
"mapping": {
"type": "integer"
}
}
},
{
"handleString": {
"match_mapping_type": "string",//通过类型映射 es数据中string默认text+keyword类型,string类型映射为keyword
"mapping": {
"type": "keyword"
}
}
}
]//动态映射
}
}
2.创建索引文档
请求:POST http://localhost:9200/lt_tao/_doc/1
{
"id" : 1,
"username" : "leitao",
"school" : "黄河学院",
"age" : 18,
"time" : "2022-08-19 15:12:00"
}
3.查看索引mapping
请求:GET http://localhost:9200/lt_tao/_mapping
{
"lt_tao": {
"mappings": {
"dynamic_templates": [
{
"handleLong": {
"match_mapping_type": "long",
"mapping": {
"type": "integer"
}
}
},
{
"handleString": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"age": {
"type": "integer"
},
"id": { //id为integer
"type": "integer"
},
"school": { //string为keyword
"type": "keyword"
},
"time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"username": { //string为keyword
"type": "keyword"
}
}
}
}
}
二、通过字段名称匹配
- 字段的名称可以是match(匹配)和unmatch(不匹配)或match_pattern(模式匹配)。
1.更新模板文件
当模板文件名称相同的时候,将对原始模板进行更新。
请求:PUT http://localhost:9200/_template/my_template_test (my_template_test->模板名称)
{
"index_patterns": [
"lt*"
], // 可以通过"lt*来适配, template字段已过期
"order": 0, // 模板的权重, 多个模板的时候优先匹配用, 值越大, 权重越高
"settings": {
"number_of_shards": 1, // 分片数量, 可以定义其他配置项
"number_of_replicas": "1", // 副本数
"refresh_interval": "5s" // 刷新时间
},
"aliases": {
"lt_test": {} // 索引对应的别名
},
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
},//固定映射
"dynamic_templates": [//注意:动态模板数组匹配规则:默认从数组第一个模板开始匹配,成功则结束往后匹配
{
"longContentString": { //动态模板名称
"match_mapping_type": "string",
"match": "desc_*", //匹配字段名称desc_开头的
"unmatch": "*_text",//不匹配字段名称_text结尾的
"mapping": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
{
"handleLong": { //动态模板名称
"match_mapping_type": "long",//通过类型映射 es数据中数字默认long类型,long类型映射为integer
"mapping": {
"type": "integer"
}
}
},
{
"handleString": {
"match_mapping_type": "string",//通过类型映射 es数据中string默认text+keyword类型,string类型映射为keyword
"mapping": {
"type": "keyword"
}
}
}
]//动态映射
}
}
2.创建索引文档
模板修改之后对已经创建的索引无效,所以必须先删除索引重新创建,删除索引:请求:DELETE http://localhost:9200/lt_tao/
请求:POST http://localhost:9200/lt_tao/_doc/1
{
"id" : 1,
"username" : "leitao",
"desc_user" : "欢迎关注,共同学习,提升自己", //desc_开头
"school" : "黄河学院",
"school_text" : "来自于长江黄河", //_text结尾
"age" : 18,
"time" : "2022-08-19 15:12:00"
}
3.查看索引mapping
请求:GET http://localhost:9200/lt_tao/_mapping
{
"lt_tao": {
"mappings": {
"dynamic_templates": [
{
"longContentString": {
"match": "desc_*",
"unmatch": "*_text",
"match_mapping_type": "string",
"mapping": {
"fields": {
"raw": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
}
}
},
{
"handleLong": {
"match_mapping_type": "long",
"mapping": {
"type": "integer"
}
}
},
{
"handleString": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"age": {
"type": "integer"
},
"desc_user": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "integer"
},
"school": {
"type": "keyword"
},
"school_text": {
"type": "keyword"
},
"time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"username": {
"type": "keyword"
}
}
}
}
}