[Elasticsearch] ES修改字段类型

Posted 一杯糖不加咖啡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Elasticsearch] ES修改字段类型相关的知识,希望对你有一定的参考价值。

参考:https://blog.csdn.net/weixin_41228362/article/details/107382647
概述
首先说明es中索引的字段类型是不可修改的,只能是重新创建一个索引并设置好mapping,然后再将老索引的数据复制过去。

操作步骤
1、创建索引,并指定mapping

PUT /web-log

   "mappings" : 
     "properties" : 
       "classMethod" : 
         "type" : "text",
         "fields" : 
           "keyword" : 
             "type" : "keyword",
             "ignore_above" : 256
           
         
       ,
       "consumeTime" : 
         "type" : "long"
       ,
       "consumeTimeType" : 
         "type" : "text",
         "fields" : 
           "keyword" : 
             "type" : "keyword",
             "ignore_above" : 256
           
         
       ,
       "date" : 
         "type" : "date",
         "format": "yyyy-M-d"
       ,
       "day" : 
         "type" : "long"
       ,
       "hour" : 
         "type" : "long"
       ,
       "ip" : 
         "type" : "text",
         "fields" : 
           "keyword" : 
             "type" : "keyword",
             "ignore_above" : 256
           
         
       ,
       "minute" : 
         "type" : "long"
       ,
       "month" : 
         "type" : "long"
       ,
       "requestArgs" : 
         "type" : "text",
         "fields" : 
           "keyword" : 
             "type" : "keyword",
             "ignore_above" : 256
           
         
       ,
       "requestId" : 
         "type" : "text",
         "fields" : 
           "keyword" : 
             "type" : "keyword",
             "ignore_above" : 256
           
         
       ,
       "requestMethod" : 
         "type" : "text",
         "fields" : 
           "keyword" : 
             "type" : "keyword",
             "ignore_above" : 256
           
         
       ,
       "second" : 
         "type" : "long"
       ,
       "threadID" : 
         "type" : "text",
         "fields" : 
           "keyword" : 
             "type" : "keyword",
             "ignore_above" : 256
           
         
       ,
       "timestamp" : 
         "type" : "long"
       ,
       "userName" : 
         "type" : "text",
         "fields" : 
           "keyword" : 
             "type" : "keyword",
             "ignore_above" : 256
           
         
       ,
       "year" : 
         "type" : "long"
       
     
   
 
  1. 将老的索引中的数据复制到新的索引中:
POST _reindex

 "source": 
   "index": "web_log"
 ,
 "dest": 
   "index": "web-log"
 

最后成功了

以上是关于[Elasticsearch] ES修改字段类型的主要内容,如果未能解决你的问题,请参考以下文章

向elasticsearch中动态添加字段报错问题和向elasticsearch中添加嵌套数据类型数据--工作学习记录

php elasticsearch (四) 索引管理 --- 2022-04-02

Elasticsearch 7.x Nested 嵌套类型查询 ES 干货

Elasticsearch 字段类型

Elasticsearch-使用映射来定义各种文档

ES 字段类型Field type