es修改數據類型


環境:
es版本:6.5.0

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
          }
        }
      }
    }
  }
}

 

-- The End --

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM