51

Elastic search mapping 不能更新解决方案 - 鸡尾酒的笔记

 4 years ago
source link: https://blog.cocktail1024.top/archives/121.html?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

增加字段的时候手残,将 float 类型的字段,加了 " , 自动识别为了 text 类型,想要修改 mapping 的时候发现 Elastic search 的 mapping 建立之后就不能更新了 !!

大体方案是 建立一个新的 索引 index_v2, 然后 reindex ,再将旧索引删掉,最后建立一个 alias 到 index_v2

talk is less,show me the code

  • 当前索引 index_v1
  • 目标索引 index_v2

第一步,建立一个新的索引 index_v2 (mappings 替换成你需要的)

curl -X PUT "http://localhost:9200/index_v2" -H 'Content-Type: application/json' -d'
{
    "mappings": {
        "content": {
            "properties": {
                "title": {
                    "type": "text",
                    "fields": {
                    "accurate": { "type": "keyword" }
                    },
                    "analyzer": "ik_smart",
                    "search_analyzer": "ik_smart",
                    "include_in_all": "true"
                },
                "content": {
                    "type": "text",
                    "analyzer": "ik_smart",
                    "search_analyzer": "ik_smart",
                    "include_in_all": "true"
                },
                "author": { "type": "keyword" },
                "category": { "type": "keyword" }
            }
        }
    }
}
'

第二步,reindex ,将 index_v1 的数据重新导入到 index_v2

curl -X POST "http://localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
{
    "source": {
        "index": "index_v1"
    },
    "dest": {
        "index": "index_v2"
    }
}
'

第三步,删除旧索引

curl -X DELETE "http://localhost:9200/index_v1"

第四步,alias 到新索引

curl -X POST "http://localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{
    "actions": [
        { "add" : { "index" : "index_v2", "alias" : "index_v1" } }
    ]
}
'

当然,这里也可以选择再重建 index_v1 索引,然后在将 index_v2 reindex 到 index_v1 (生产不建议这样做,如果数据量比较大会耗时比较多,测试环境可以这样玩)

删除别名的方式

curl -XPOST 'http://localhost:9200/_aliases' -H 'Content-Type: application/json' -d'
{

"actions": [
    {"remove": {"index": "index_v2", "alias": "index_v1"}}
]

Elasticsearch如何更新mapping https://blog.csdn.net/Sympeny/article/details/77650414


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK