文章目录

前言
博主介绍:✌目前全网粉丝4W+,csdn博客专家、Java领域优质创作者,博客之星、阿里云平台优质作者、专注于Java后端技术领域。
涵盖技术内容:Java后端、大数据、算法、分布式微服务、中间件、前端、运维等。
博主所有博客文件目录索引:博客目录索引(持续更新)
CSDN搜索:长路
视频平台:b站-Coder长路
背景
年增长千万条记录的情况,使用时间序列索引(例如按年或按月创建新索引)是比拆分单个旧索引更推荐的方案。这样做的好处是:
- 每年或每月的新索引都可以根据当时的数据量重新规划分片数。
- 管理起来更灵活,数据老化后可以直接关闭或删除整个索引,释放资源。
- 完全避免了拆分大索引带来的复杂性和风险。
优化方案:时间分区 + 全局视图别名
详细实现思路
一、创建索引模板
问题: 传统方案中,每个索引需要手动创建映射,容易出现:
- 映射不一致导致查询异常
- 字段类型冲突
- 维护成本高
演进: 从ES 7.8开始引入索引模板,实现自动化映射管理。
shell
PUT _index_template/file_document_template
{
"index_patterns": ["file_document-*"],
"template": {
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1,
"refresh_interval": "1s"
},
"mappings": {
"properties": {
"fileName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"fileContent": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"fileType": {
"type": "keyword"
},
"fileSize": {
"type": "long"
},
"createTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"updateTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"author": {
"type": "keyword"
},
"tags": {
"type": "keyword"
}
}
}
},
"priority": 200
}
参数详解:
- index_patterns : 匹配模式
file_document-*,所有符合该模式的索引自动应用此模板。 - number_of_shards: 分片数设为2,平衡查询性能与资源消耗
- number_of_replicas: 副本数设为1,保证数据高可用
- refresh_interval: 刷新间隔1秒,平衡实时性与性能
- analyzer: 使用ik_max_word细粒度分词,适合中文
- search_analyzer: 使用ik_smart智能分词,提高搜索准确率
- date format: 支持多种时间格式,增强兼容性
- priority: 优先级200,确保此模板优先应用。
二、时间分区索引设计
传统单索引方案问题:
shell
# 旧方案 - 单索引存储所有数据
PUT /file_document
# 随着数据增长到千万级别,出现:
# 1. 分片过大,查询变慢
# 2. 重建索引风险高
# 3. 数据老化困难
拆分方案演进:
- 按数量拆分 :
index-001,index-002- 难以管理时间范围 - 按业务拆分 :
index-user,index-order- 不适合时间序列数据 - 按时间拆分 :
index-2024-01- 最优解 for 时间序列数据
实际代码如下:
shell
PUT /file_document-2024-01
PUT /file_document-2024-02
PUT /file_document-2024-03
PUT /file_document-2024-04
PUT /file_document-2025-01
- file_document-{year}-{month}: 按年月分区
- 2024-01: 2024年1月数据
- 2025-01: 2025年1月数据
三、智能别名系统
直接操作索引的问题:
java
// 传统方式 - 硬编码索引名
client.prepareSearch("file_document-2024-01", "file_document-2024-02")
.setQuery(...)
// 问题:
// 1. 代码耦合严重
// 2. 索引变更需要修改代码
// 3. 容易遗漏索引
别名方案优势:
- 解耦应用逻辑与物理索引
- 支持索引无缝切换
- 简化查询逻辑
示范如下:
shell
POST /_aliases
{
"actions": [
{
"add": {
"index": "file_document-2025-01",
"alias": "file_document_current"
}
},
{
"add": {
"index": "file_document-*",
"alias": "file_document_all"
}
},
{
"add": {
"index": "file_document-2024-*",
"alias": "file_document_2024"
}
},
{
"add": {
"index": "file_document-2025-*",
"alias": "file_document_2025"
}
}
]
}
别名策略:
- file_document_current: 指向当前活跃索引,用于写入和实时查询
- file_document_all: 包含所有索引,用于全量搜索
- file_document_2024: 2024年所有数据,用于年度分析
- file_document_2025: 2025年所有数据,用于当前年度查询
四、插入索引数据
java
# 插入2024年1月数据
POST /file_document-2024-01/_bulk
{"index":{}}
{"fileName":"2024年1月项目报告","fileContent":"这是2024年1月份的技术项目报告文档,包含详细的技术方案和实施计划","fileType":"pdf","fileSize":2048576,"createTime":"2024-01-15 10:30:00","updateTime":"2024-01-20 14:25:00","author":"张三","tags":["项目","报告","技术"]}
{"index":{}}
{"fileName":"Q1季度总结","fileContent":"第一季度工作季度总结报告,包含各项业务数据和分析","fileType":"docx","fileSize":1536000,"createTime":"2024-01-25 16:45:00","updateTime":"2024-01-28 09:15:00","author":"李四","tags":["季度","总结","业务"]}
# 插入2024年2月数据
POST /file_document-2024-02/_bulk
{"index":{}}
{"fileName":"2月技术方案","fileContent":"二月份制定的新技术方案文档,涉及架构优化和性能提升","fileType":"pdf","fileSize":1892352,"createTime":"2024-02-10 11:20:00","updateTime":"2024-02-15 13:40:00","author":"王五","tags":["技术","方案","架构"]}
{"index":{}}
{"fileName":"春节值班安排","fileContent":"2024年春节期间值班人员安排表和联系方式","fileType":"xlsx","fileSize":512000,"createTime":"2024-02-05 08:30:00","updateTime":"2024-02-06 10:15:00","author":"赵六","tags":["行政","值班","春节"]}
# 插入2024年3月数据
POST /file_document-2024-03/_bulk
{"index":{}}
{"fileName":"3月项目进度报告","fileContent":"三月份各项目进度汇总报告,包含里程碑完成情况","fileType":"pdf","fileSize":2252800,"createTime":"2024-03-20 14:50:00","updateTime":"2024-03-25 16:30:00","author":"张三","tags":["项目","进度","报告"]}
{"index":{}}
{"fileName":"技术培训材料","fileContent":"春季技术培训的相关材料和演示文档","fileType":"pptx","fileSize":3072000,"createTime":"2024-03-08 09:15:00","updateTime":"2024-03-12 11:45:00","author":"王五","tags":["培训","技术","材料"]}
# 插入2025年1月数据
POST /file_document-2025-01/_bulk
{"index":{}}
{"fileName":"2025年度规划","fileContent":"2025年度公司发展规划和技术路线图","fileType":"pdf","fileSize":2893568,"createTime":"2025-01-10 13:25:00","updateTime":"2025-01-15 15:40:00","author":"CEO","tags":["年度","规划","战略"]}
{"index":{}}
{"fileName":"新技术调研报告","fileContent":"关于AI和机器学习新技术的调研分析报告","fileType":"docx","fileSize":1982464,"createTime":"2025-01-18 10:20:00","updateTime":"2025-01-22 14:35:00","author":"技术部","tags":["AI","调研","技术"]}
验证数据分布
shell
# 查看各索引文档数量
GET _cat/indices/file_document-*?v&s=index
# 查看别名配置
GET _cat/aliases/file_document_*?v

各类查询方式
场景1:搜索全部数据(跨年搜索)
shell
# 搜索所有年份包含"项目报告"的文件
GET /file_document_all/_search
{
"query": {
"match": {
"fileName": "项目报告"
}
},
"sort": [
{
"createTime": {
"order": "desc"
}
}
]
}
场景2:搜索特定年份数据
shell
# 只搜索2024年的技术相关文件
GET /file_document_2024/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"fileContent": "技术"
}
}
]
}
},
"aggs": {
"file_type_stats": {
"terms": {
"field": "fileType"
}
}
}
}
场景3:跨年时间范围搜索
shell
# 搜索2024年6月到2025年3月的数据(跨年搜索)
GET /file_document_all/_search
{
"query": {
"range": {
"createTime": {
"gte": "2024-06-01",
"lte": "2025-03-31"
}
}
},
"sort": [
{
"createTime": {
"order": "asc"
}
}
]
}
场景4:复杂条件组合搜索
shell
# 复杂搜索:技术相关 + PDF文件 + 时间范围
GET /file_document_all/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"fileContent": "技术方案"
}
},
{
"term": {
"fileType": "pdf"
}
},
{
"range": {
"createTime": {
"gte": "2024-01-01",
"lte": "2025-12-31"
}
}
}
]
}
},
"highlight": {
"fields": {
"fileContent": {}
}
}
}
场景5:聚合分析
shell
# 按作者聚合分析文档数量
GET /file_document_all/_search
{
"size": 0,
"aggs": {
"authors": {
"terms": {
"field": "author",
"size": 10
},
"aggs": {
"total_size": {
"sum": {
"field": "fileSize"
}
}
}
},
"monthly_trend": {
"date_histogram": {
"field": "createTime",
"calendar_interval": "month",
"format": "yyyy-MM"
}
}
}
}
维护索引方式
添加新月份索引
shell
# 当需要创建新月份索引时
PUT /file_document-2025-02
# 更新别名,将新索引加入相应别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "file_document-2025-02",
"alias": "file_document_current"
}
},
{
"remove": {
"index": "file_document-2025-01",
"alias": "file_document_current"
}
},
{
"add": {
"index": "file_document-2025-02",
"alias": "file_document_2025"
}
},
{
"add": {
"index": "file_document-2025-02",
"alias": "file_document_all"
}
}
]
}
资料获取
大家点赞、收藏、关注、评论啦~
精彩专栏推荐订阅:在下方专栏👇🏻
- 长路-文章目录汇总(算法、后端Java、前端、运维技术导航):博主所有博客导航索引汇总
- 开源项目Studio-Vue---校园工作室管理系统(含前后台,SpringBoot+Vue):博主个人独立项目,包含详细部署上线视频,已开源
- 学习与生活-专栏:可以了解博主的学习历程
- 算法专栏:算法收录
更多博客与资料可查看👇🏻获取联系方式👇🏻,🍅文末获取开发资源及更多资源博客获取🍅