ES修改字段类型

在ES中如何修改字段类型?

ES修改字段类型
环境

Elasticsearch版本:7.13.2

概述

ES 索引的字段类型是不可修改的,所以无法从原索引下手。

流程:旧索引备份,删除旧索引,重新新建索引,把备份索引的数据复制到新建us索引。

查看索引mapping

GET person-record-139/_mapping

返回:
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 0,
    "index": {
      "max_result_window": 2147483647
    },
    "analysis": {
      "tokenizer": {
        "ik_pinyin": {
          "type": "pinyin",
          "keep_first_letter": true,
          "keep_full_pinyin": false,
          "keep_joined_full_pinyin": true,
          "keep_original": true,
          "limit_first_letter_length": 10,
          "keep_none_chinese": true,
          "keep_none_chinese_together": true,
          "none_chinese_pinyin_tokenize": false,
          "keep_none_chinese_in_first_letter": false,
          "lowercase": true,
          "trim_whitespace": false,
          "remove_duplicated_term": true,
          "keep_separate_first_letter": false
        }
      },
      "analyzer": {
        "custom_ik_smart": {
          "type": "custom",
          "tokenizer": "ik_smart",
          "char_filter": [
            "html_strip"
          ]
        },
        "custom_ik_max_word": {
          "type": "custom",
          "tokenizer": "ik_max_word",
          "char_filter": [
            "html_strip"
          ]
        },
        "pinyin_analyzer": {
          "tokenizer": "ik_pinyin"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "name": {
        "type": "text",
        "analyzer": "custom_ik_max_word",
        "fields": {
          "pinyin": {
            "type": "text",
            "store": false,
            "term_vector": "with_offsets",
            "analyzer": "pinyin_analyzer"
          },
          "keyword": {
            "type": "keyword",
            "ignore_above": 13011
          }
        }
      },
      "sex": {
        "type": "keyword"
      },
      "similarity": {
        "type": "double"
      },
      "createTime": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "featureData": {
        "type": "dense_vector",
        "dims": 512
      },
      "remark": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

字段remark是text类型的,打算修改为keyword类型。

一、备份索引
POST _reindex
{
  "source": {
    "index": "record"
  },
  "dest": {
    "index": "record-bak"
  }
}

source.index: 旧索引名称
dest.index: 新索引名称

二、删除旧索引
DELETE record
三、重建索引

把原mapping中remark字段修改成以下,并执行PUT请求。

PUT record
...
"remark": {
  "type": "keyword"
}
...
四、恢复数据
POST _reindex
{
  "source": {
    "index": "record-bak"
  },
  "dest": {
    "index": "record"
  }
}

查看record mapping信息,发现remark字段的类型已经修改为keyword,到此修改工作已完成。