【ElasticSearch】索引模板(一)
背景
由于需要存储风控审核相关的数据,审核数据原数据是保存在MySQL,数据量太大,按月分表,分表后导致后台无法跨月查询,因此将数据同步到ElasticSearch中
为了合理的划分索引的大小,需要将对应的ElasticSearch索引也按月创建索引,将对应的MySQL月份表数据同步到ES月份索引中,所有的ElasticSearch 按月创建的索引使用同一个别名,后台查询统一使用索引别名查询,这样就解决了MySQL按月分表后无法进行跨月查询的问题
测试代码
为需要按月创建的索引,创建指定的索引模板
1.创建模板 test_template,指定索引别名为test,以及对应的mapping
template属性表示 在创建以 “test”开头的索引时,会自动使用该模板
# 模板 PUT /_template/test_template { "template": "test*", "settings": { "number_of_shards": 10 }, "mappings": { "data": { "_source": { "enabled": false }, "properties": { "name": { "type": "keyword" }, "id": { "type": "long" } } } }, "aliases": { "test": {} } }
模板创建完成后,可以使用API查询模板详情
GET /_template/test_template
2.创建索引 test2和test1
PUT test1
PUT test2
由于索引test1和索引test2匹配到模板test_template,会自动套用该模板属性
因此查询test1和test2索引详情可以看到,与模板属性一致
test1索引详情
{ "test1" : { "aliases" : { "test" : { } }, "mappings" : { "data" : { "_source" : { "enabled" : false }, "properties" : { "id" : { "type" : "long" }, "name" : { "type" : "keyword" } } } }, "settings" : { "index" : { "creation_date" : "1646624507395", "number_of_shards" : "10", "number_of_replicas" : "1", "uuid" : "5qbPh-QsQ2SNzKqNGL401Q", "version" : { "created" : "6080799" }, "provided_name" : "test1" } } } }
test2索引详情
{ "test2" : { "aliases" : { "test" : { } }, "mappings" : { "data" : { "_source" : { "enabled" : false }, "properties" : { "id" : { "type" : "long" }, "name" : { "type" : "keyword" } } } }, "settings" : { "index" : { "creation_date" : "1646624657372", "number_of_shards" : "10", "number_of_replicas" : "1", "uuid" : "t0irKTFQT9qU_F5m_v7RlA", "version" : { "created" : "6080799" }, "provided_name" : "test2" } } } }
3.为test1和test2添加数据
POST test1/_doc/1 { "id":1, "name":"test1-aaaa" } POST test1/_doc/2 { "id":2, "name":"test1-bbbb" } POST test2/_doc/1 { "id":1, "name":"test2-aaaa" } POST test2/_doc/2 { "id":2, "name":"test2-bbbb" }
4.通过别名test查询,可以查询test1和test2全部数据
GET test/_search
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 40, "successful" : 40, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 4, "max_score" : 1.0, "hits" : [ { "_index" : "test1", "_type" : "data", "_id" : "1", "_score" : 1.0 }, { "_index" : "test2", "_type" : "data", "_id" : "1", "_score" : 1.0 }, { "_index" : "test1", "_type" : "data", "_id" : "2", "_score" : 1.0 }, { "_index" : "test2", "_type" : "data", "_id" : "2", "_score" : 1.0 } ] } }
分类:
ElasticSearch
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2020-03-07 【并发编程】ThreadLocal
2020-03-07 【SpringBoot】SpringBoot 处理后端返回的小数(全局配置 + 定制化配置)