配置项作用 #
index.codec 配置项指定索引使用的 Lucene 压缩编解码器(Codec)。编解码器决定了数据在磁盘上的压缩格式,影响存储空间、索引性能和查询性能。
配置项类型 #
该配置项为静态配置,只在写入新段时生效,不支持实时更新。
默认值 #
default(LZ4 压缩)
是否必需 #
可选配置项(有默认值)
可选值 #
| 值 | 压缩算法 | 说明 |
|---|---|---|
default | LZ4 | 默认压缩,平衡性能和压缩率 |
best_compression | DEFLATE | 最高压缩率,性能较低 |
lucene_default | Lucene 默认 | Lucene 的默认编解码器 |
配置格式 #
# 创建索引时设置
PUT /my_index
{
"settings": {
"index.codec": "best_compression"
}
}
# 索引模板中设置
PUT /_template/compressed_template
{
"index_patterns": ["logs-*"],
"settings": {
"index.codec": "best_compression"
}
}
编解码器对比 #
| 编解码器 | 存储空间 | 索引速度 | 查询速度 | CPU使用 |
|---|---|---|---|---|
| default | 中等 | 快 | 快 | 低 |
| best_compression | 最小 | 慢 | 慢 | 高 |
| lucene_default | 中等 | 中等 | 中等 | 中等 |
使用场景 #
1. 默认配置(推荐大多数场景) #
index.codec: default
特点:
- LZ4 压缩算法
- 平衡压缩率和性能
- 适合大多数工作负载
2. 存储优化(日志/历史数据) #
index.codec: best_compression
特点:
- DEFLATE 压缩算法
- 最高压缩率(可节省 30-50% 空间)
- 索引和查询性能降低
- CPU 使用率增加
适用场景:
- 日志数据
- 历史档案
- 存储成本敏感
- 写入不频繁
3. 性能优先(热数据) #
index.codec: default
适用场景:
- 频繁写入的索引
- 实时搜索
- 低延迟要求
热温冷架构应用 #
# 热数据(频繁访问)
PUT /logs-hot-*
{
"settings": {
"index.codec": "default"
}
}
# 温数据(偶尔访问)
PUT /logs-warm-*
{
"settings": {
"index.codec": "default"
}
}
# 冷数据(归档)
PUT /logs-cold-*
{
"settings": {
"index.codec": "best_compression"
}
}
使用示例 #
创建索引时设置:
# 使用最佳压缩
PUT /archive_index
{
"settings": {
"index.codec": "best_compression",
"index.number_of_shards": 1,
"index.number_of_replicas": 0
}
}
通过 ILM 自动应用:
PUT /_ilm_POLICY/logs_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_size": "50GB"
}
}
},
"warm": {
"actions": {
"forcemerge": {
"max_num_segments": 1
}
}
},
"cold": {
"actions": {
"set_codec": {
"codec": "best_compression"
}
}
}
}
}
}
性能影响分析 #
| 编解码器 | 压缩率 | 索引吞吐量 | 查询延迟 | 磁盘 I/O | CPU 使用 |
|---|---|---|---|---|---|
| default | 1.5-2x | 高 | 低 | 中 | 低 |
| best_compression | 2-3x | 低-中 | 中 | 低 | 高 |
存储空间对比 #
假设原始数据为 100GB:
使用 default:
压缩后: ~50-67GB
压缩时间: 快
查询时间: 快
使用 best_compression:
压缩后: ~33-50GB
压缩时间: 慢 (2-3倍)
查询时间: 慢 (10-20%)
推荐设置建议 #
| 数据类型 | 推荐编解码器 | 说明 |
|---|---|---|
| 实时数据 | default | 性能优先 |
| 日志数据 | default 或 best_compression | 根据存储成本决定 |
| 历史归档 | best_compression | 存储空间优先 |
| 指标数据 | default | 高频写入 |
| 文档全文 | default | 查询性能优先 |
监控建议 #
# 查看索引配置
GET /my_index/_settings?filter_path=*.index.codec
# 查看索引大小
GET /_cat/indices/my_index?v&h=index,store.size,pri.store.size
# 查看段信息
GET /my_index/_segments?pretty
常见问题 #
问题 1:无法修改现有索引的编解码器
原因: 编解码器只能在创建新段时设置
解决方案:
- 重建索引
POST /_reindex
{
"source": { "index": "old_index" },
"dest": {
"index": "new_index",
"settings": {
"index.codec": "best_compression"
}
}
}
- 使用 Force Merge
# 强制合并会使用新的编解码器重写段
PUT /my_index/_settings
{
"index.codec": "best_compression"
}
POST /my_index/_forcemerge?max_num_segments=1
问题 2:索引性能下降
原因: 使用了 best_compression
解决方案:
# 改回默认编解码器
PUT /my_index/_settings
{
"index.codec": "default"
}
问题 3:存储空间不足
解决方案:
# 使用最佳压缩
PUT /my_index/_settings
{
"index.codec": "best_compression"
}
# 然后强制合并
POST /my_index/_forcemerge?max_num_segments=1
注意事项 #
- 非实时更新:修改设置后需要强制合并才能生效
- 性能权衡:best_compression 会降低性能
- CPU 消耗:高压缩率需要更多 CPU
- 段重写:修改编解码器需要重写所有段
- 规划优先:应在索引创建时确定合适的编解码器
- 测试验证:生产环境使用前应进行性能测试





