You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ElasticSearch中将同一索引的两个类型合并为一个类型

合并Elasticsearch索引中的TypeA和TypeB为TypeC的步骤

步骤1:创建TypeC的映射

先在原索引my_index中添加TypeC的映射,确保结构与TypeA/B完全一致:

PUT /my_index/_mapping/TypeC
{
  "properties" : {
    "autoid" : {
      "type" : "integer"
    },
    "database" : {
      "type" : "keyword"
    },
    "database_version" : {
      "type" : "keyword"
    },
    "error_message" : {
      "type" : "keyword"
    },
    "failure_date" : {
      "type" : "date",
      "format" : "yyyy-MM-dd HH:mm:ss.SSSSSS"
    },
    "id_sequence" : {
      "type" : "integer"
    },
    "id_test" : {
      "type" : "integer"
    },
    "operator" : {
      "type" : "keyword"
    },
    "part_number" : {
      "type" : "keyword"
    },
    "read_value" : {
      "type" : "keyword"
    },
    "serial_number" : {
      "type" : "keyword"
    },
    "site" : {
      "type" : "keyword"
    },
    "test_software_version" : {
      "type" : "keyword"
    },
    "test_station" : {
      "type" : "keyword"
    },
    "test_type" : {
      "type" : "keyword"
    },
    "timestamp" : {
      "type" : "date",
      "format" : "yyyy-MM-dd HH:mm:ss.SSSSSS"
    },
    "transaction_date" : {
      "type" : "date",
      "format" : "yyyy-MM-dd HH:mm:ss.SSSSSS"
    },
    "waited_value" : {
      "type" : "keyword"
    }
  }
}

步骤2:迁移TypeA数据到TypeC

使用_reindex API将TypeA的所有数据迁移到TypeC:

POST _reindex
{
  "source": {
    "index": "my_index",
    "type": "TypeA"
  },
  "dest": {
    "index": "my_index",
    "type": "TypeC"
  }
}

步骤3:迁移TypeB数据到TypeC

重复执行_reindex迁移TypeB的数据:

POST _reindex
{
  "source": {
    "index": "my_index",
    "type": "TypeB"
  },
  "dest": {
    "index": "my_index",
    "type": "TypeC"
  }
}

步骤4:验证迁移结果

执行查询确认TypeC包含所有数据,可对比原类型的文档总数:

GET my_index/TypeC/_search
{
  "size": 20
}

步骤5:清理原类型(可选)

确认数据无误后,删除原TypeA和TypeB的映射及对应数据:

DELETE /my_index/_mapping/TypeA
DELETE /my_index/_mapping/TypeB

注意事项

  • 若你的Elasticsearch版本为7.x及以上,已不再支持单索引多类型,此时需创建新索引(如my_index_new),将TypeA和TypeB的数据统一reindex到新索引的默认类型_doc下,再替换原索引。
  • 迁移前建议备份原索引数据,避免意外丢失。

内容的提问来源于stack exchange,提问作者Ftu_Sneaky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 11:47:08