首页
看点啥
插画图片
首页 科技看点 Elasticsearch synthetic _source 怎么开启才能减少索引占用?

Elasticsearch synthetic _source 怎么开启才能减少索引占用?

2026-11-25 0

按 ID 获取文档或读取搜索命中时,Elasticsearch 可以返回 _source。如果新索引的空间预算有限,可以启用 synthetic,让引擎按需从 doc_values 等结构重建文档。建议先用样本核对主分片尺寸和读取结果。

1. 新索引直接启用 synthetic

以下请求可在 Kibana 的 Dev Tools Console 中逐条运行,示例用 Elasticsearch 9.5.0 和 Trial 许可实测。在自建 Elasticsearch 8.17 及之后的版本中,此功能属于 Enterprise 订阅。下面使用标准索引和索引设置写法,单分片、零副本用于本地对照。

PUT source-synthetic
{
  "settings": {
    "index.mode": "standard",
    "index.number_of_shards": 1,
    "index.number_of_replicas": 0,
    "index.mapping.source.mode": "synthetic"
  },
  "mappings": {
    "properties": {
      "service": {"type": "keyword"},
      "tags": {"type": "keyword"},
      "duration_ms": {"type": "integer"}
    }
  }
}
PUT source-synthetic/_doc/1?refresh=true
{"service": "checkout-1", "tags": ["write", "read", "write"], "duration_ms": 17}

GET source-synthetic/_source/1

示例读回的 keyword 数组按字典序排列,并合并了重复的 write。字段顺序也可能变化。若数据包含对象数组,还需核对数组元素之间的原始分组有没有改变。

{"duration_ms": 17, "service": "checkout-1", "tags": ["read", "write"]}

优点。像示例中常见的 keyword 和 integer 字段可以利用已有结构重建,减少为文档内容单独占用的空间。

限制。读取完整 _source 要付出重建开销。若查询只需要几个具备 doc_values 的字段,可以关闭返回 _source,再按字段取值。前面的写入请求已刷新索引,可以直接搜索这条文档。

GET source-synthetic/_search
{
  "size": 1,
  "_source": false,
  "docvalue_fields": ["service", "duration_ms"]
}

2. 只保留依赖原始顺序的数组

如果应用要把 tags 当作有序列表,可继续使用 synthetic,在该字段的映射上加 synthetic_source_keep: arrays。把同一条文档写入下面的新索引,再读取 GET source-keep/_source/1。

PUT source-keep
{
  "settings": {
    "index.mode": "standard",
    "index.number_of_shards": 1,
    "index.number_of_replicas": 0,
    "index.mapping.source.mode": "synthetic"
  },
  "mappings": {
    "properties": {
      "service": {"type": "keyword"},
      "tags": {"type": "keyword", "synthetic_source_keep": "arrays"},
      "duration_ms": {"type": "integer"}
    }
  }
}

在本例中,tags 读回为 ["write", "read", "write"]。启用保留会增加存储开销,复杂嵌套数组的原始表示仍可能变化。若整个字段或对象都依赖原始形状,可以评估更严格的 synthetic_source_keep: all,并用实际数据重新测量。

3. 迁移旧索引时先建目标索引

index.mapping.source.mode 是静态索引设置。将已有索引迁到 synthetic 时,先用第一节的设置和业务映射创建一个空目标索引,例如 source-migrated,再复制文档。旧索引需要保有可读的 _source。若从头跑示例,先将第一节的索引名换成 source-stored,模式改成 stored,并写入同一条文档。

POST _reindex?refresh=true
{
  "source": {"index": "source-stored"},
  "dest": {"index": "source-migrated"}
}

GET source-migrated/_count
GET source-migrated/_source/1

优点和限制。并行保留旧索引方便比对和回退,迁移期间则要为两份数据预留空间。检查任务的 failures、文档数和代表性文档,并在切换读写别名前处理并发写入,例如安排短暂停写窗口或补齐增量。

按主分片字节数比较三种方式

在同一个本地单节点上,三个索引使用相同的字段类型和默认编解码器,各写入 20,000 条含服务名、重复标签和时长的测试数据。实验索引停止写入后合并为单段,再用下面的命令查看 primaries.store.size_in_bytes。请对自己的测试数据使用一致的映射、分片和合并条件。

GET source-stored,source-synthetic,source-keep/_stats/store?filter_path=indices.*.primaries.store.size_in_bytes
方式主分片占用第 1 条 tags适用场景
stored617,873 字节["write", "read", "write"]读取原始文档
synthetic413,726 字节["read", "write"]能接受重建后的结构
synthetic +字段级 arrays414,051 字节["write", "read", "write"]少数字段依赖数组顺序或重复值

这组数据中,直接启用 synthetic 的主分片占用减少 33.0%。换一套字段类型、数据分布或保留规则,测得的空间收益也会变化。上线前,优先比较自己的主分片尺寸和整文档读取延迟。接受重建结果时直接启用 synthetic,只对少数字段有结构要求时按字段保留,需要读取原始 JSON 时选 stored。

喜欢(0)

上一篇

Elasticsearch 模型部署的 number_of_allocations 怎么更新?

Elasticsearch 模型部署的 number_of_allocations 怎么更新?

下一篇

AI视频克隆生成器怎么选?2025年10款数字人克隆工具对比

AI视频克隆生成器怎么选?2025年10款数字人克隆工具对比
猜你喜欢