天天看點

記一次ElasticSearch 更改 mapping 字段類型的過程

我的個人部落格:逐漸前行STEP

首先,es不支援直接更改mappinng,是以,更改 mapping 實質上是重建索引。

操作步驟如下:

1、為目前這個索引

old_index

設定一個别名

my_index

curl -XPOST localhost:9200/_aliases -d '  
{  
    "actions": [  
        { "add": {  
            "alias": "my_index",  
            "index": "old_index"  
        }}  
    ]  
}  '
           

2、通過别名

my_inndex

通路索引

old_index

3、重建一個新的索引

new_index

,在此時使用需要的字段屬性;

curl -XPUT localhost:9200/new_index
{
    "mappings": {
        "doc": {
            "properties": {
                "id": {
                    "type": "long"
                },
                "user_id": {
                    "type": "long"
                }
            }
        }
    },
    "settings": {
        "index": {
            "number_of_shards": "5",
            "number_of_replicas": "1"
        }
    }
}
           

4、遷移舊的索引

old_index

資料到新的索引

new_index

上;

curl -XPOST localhost:9200/_reindex
{
	"source":{
		"index":"old_index"
	},
	"dest":{
		"index":"new_index"
	}
}
           

5、為索引

new_index

設定一個别名為

my_index

,同時删除該别名對舊索引

old_index

的指向:

curl -XPOST localhost:9200/_aliases -d '  
{  
    "actions": [  
        { "remove": {  
            "alias": "my_index",  
            "index": "old_index"  
        }},  
        { "add": {  
            "alias": "my_index",  
            "index": "new_index"  
        }}  
    ]  
}  '
           

6、删除索引

old_index

curl -XDELETE localhost:9200/old_index  
           

繼續閱讀