创建ElasticSearch索引和修改Mapping及字段类型

Elasticsearch怎么修改索引字段类型?

由于ElasticSearch没有像mysql一样可以直接字段数据类型的方法,因此需要通过创建中间索引:data_index_1,备份数据到中间索引:data_index_1,然后删除原索引: data_index,重新创建正确数据类型索引:data_index,再把中间索引:data_index_1的数据备份到新创建索引:data_index。语句通过kibana的 dev_tools/console 执行。

操作步骤如下:

复制代码

  1. 创建一个中间索引
  2. 向中间索引备份源索引的数据(mapping)
  3. 查询确认数据是否copy过去
  4. 删除有问题的索引
  5. 重新创建同名的索引(★字段类型修改正确★)
  6. 从中间索引还原到源索引的数据
  7. 删除中间索引

在kibana执行:

  1. 创建中间索引并指定mapping(字段类型)
PUT tmp_gd_whole_real_start_stop
{
  "mappings" : {
    "properties" : {
        "@timestamp" : {
          "type" : "date"
        },
        "applied_time" : {
          "type" : "date"
        },
        "capacity" : {
          "type" : "long"
        },
        "crew" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "data_source" : {
          "type" : "long"
        },
        "plant_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "province" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "total_elec" : {
          "type" : "float"
        },
        "total_hours" : {
          "type" : "float"
        },
        "total_load_ratio" : {
          "type" : "float"
        },
        "type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
  }
}

执行结果如下:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "tmp_gd_whole_real_start_stop"
}

  1. 向中间索引备份源索引的数据
POST _reindex
{
  "source": {
    "index": "gd_whole_real_start_stop"
  },
  "dest": {
    "index": "tmp_gd_whole_real_start_stop"
  }
}

执行结果如下:

{
  "took" : 64,
  "timed_out" : false,
  "total" : 5,
  "updated" : 0,
  "created" : 5,
  "deleted" : 0,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

  1. 查询确认数据是否copy过去
GET tmp_gd_whole_real_start_stop/_search

发现已经有了数据。

  1. 删除有问题的原索引
DELETE gd_whole_real_start_stop

执行结果如下:

{
  "acknowledged" : true
}
  1. 重新创建同名的索引(★字段类型修改正确★)
PUT gd_whole_real_start_stop
{
  "mappings" : {
    "properties" : {
        "@timestamp" : {
          "type" : "date"
        },
        "applied_time" : {
          "type" : "date"
        },
        "capacity" : {
          "type" : "long"
        },
        "crew" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "data_source" : {
          "type" : "long"
        },
        "plant_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "province" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "total_elec" : {
          "type" : "float"
        },
        "total_hours" : {
          "type" : "float"
        },
        "total_load_ratio" : {
          "type" : "float"
        },
        "type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
  }
}
  1. 从中间索引还原到源索引的数据
POST _reindex
{
  "source": {
    "index": "tmp_gd_whole_real_start_stop"
  },
  "dest": {
    "index": "gd_whole_real_start_stop"
  }
}
  1. 删除中间索引
DELETE tmp_gd_whole_real_start_stop
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值