📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

配置项作用 #

index.codec 配置项指定索引使用的 Lucene 压缩编解码器(Codec)。编解码器决定了数据在磁盘上的压缩格式,影响存储空间、索引性能和查询性能。

配置项类型 #

该配置项为静态配置,只在写入新段时生效,不支持实时更新

默认值 #

default(LZ4 压缩)

是否必需 #

可选配置项(有默认值)

可选值 #

压缩算法说明
defaultLZ4默认压缩,平衡性能和压缩率
best_compressionDEFLATE最高压缩率,性能较低
lucene_defaultLucene 默认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/OCPU 使用
default1.5-2x
best_compression2-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:无法修改现有索引的编解码器

原因: 编解码器只能在创建新段时设置

解决方案:

  1. 重建索引
POST /_reindex
{
  "source": { "index": "old_index" },
  "dest": {
    "index": "new_index",
    "settings": {
      "index.codec": "best_compression"
    }
  }
}
  1. 使用 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

注意事项 #

  1. 非实时更新:修改设置后需要强制合并才能生效
  2. 性能权衡:best_compression 会降低性能
  3. CPU 消耗:高压缩率需要更多 CPU
  4. 段重写:修改编解码器需要重写所有段
  5. 规划优先:应在索引创建时确定合适的编解码器
  6. 测试验证:生产环境使用前应进行性能测试