es修改数据类型

es创建好了mapping后是不允许修改字段类型的,要是我们想修改字段类型怎么办呢,我们可以采用reindex的方法实现,就是创建一个新的mapping,里面的字段类型按照新的类型定义,然后使用reindex的方法把原来的数据拷贝到新的index下面。

1.查看原来的mapping

[esuser@localhost ~]$ curl -u elastic:elastic -H "Content-Type: application/json" -XGET "http://192.168.1.85:9200/db_customer/_mappings?pretty=true"
{
  "db_customer" : {
    "mappings" : {
      "tb_test" : {
        "properties" : {
          "name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

可以看到tb_test的字段name为text类型,我想将其修改成keyword类型

2.创建新的index和mapping

创建一个新的index
curl -u elastic:elastic -H 'Content-Type: application/json' -XPUT "http://192.168.1.85:9200/copy01_db_customer"

创建一个mapping
curl -u elastic:elastic -H 'Content-Type: application/json' -XPOST "http://192.168.1.85:9200/copy01_db_customer/tb_test/_mapping?pretty" -d ' 
{
    "tb_test": {
            "properties": {
                "name": {
                    "type": "keyword",
                    "store": "true"
                }
            }
        }
  }
'

这里创建了一个新的index叫做opy01_db_customer,相应的tb_test mapping 字段name 为keyword类型

3.数据同步


curl -u elastic:elastic -X POST "192.168.1.85:9200/_reindex" -H 'Content-Type: application/json' -d'
{
  "source": {
    "index": "db_customer"
  },
  "dest": {
    "index": "copy01_db_customer"
  }
}'

4.再次查看新index结构

[esuser@localhost ~]$ curl -u elastic:elastic -H "Content-Type: application/json" -XGET "http://192.168.1.85:9200/copy01_db_customer/_mappings?pretty=true"
{
  "copy01_db_customer" : {
    "mappings" : {
      "tb_test" : {
        "properties" : {
          "name" : {
            "type" : "keyword",
            "store" : true
          }
        }
      }
    }
  }
}
原文地址:https://www.cnblogs.com/blogabc/p/12664274.html