Elasticsearch 更新字段映射 mapping

Elasticsearch 不支持现有字段映射更新。可以通过正确创建映射一个新的索引,然后将原索引上的数据复制到新的索引上,再将 alias 指向新 indices。然后再删除原索引。

  1. 将原索引 test 添加 alias
    curl -X POST "http://192.168.1.101:9200/_aliases?pretty" -H 'Content-Type: application/json' -d'
    {
      "actions": [
        {
          "add": {
            "index": "test_source",
            "alias": "test"
          }
        }
      ]
    }
    '
    

    curl -X PUT "http://192.168.1.101:9200/test_source/_alias/test?pretty"
    
  2. 创建新索引 test_new
    curl -X PUT "http://192.168.1.101:9200/test_new?pretty"
    curl -X POST "http://192.168.1.101:9200/test_new/_mapping?pretty" -H 'Content-Type: application/json' -d'
    {
        "properties": {
            "title": {
                "type": "text",
                "analyzer":"ik_max_word",
                "search_analyzer":"ik_smart"
            },
            "content": {
                "type": "text",
                "analyzer":"ik_max_word",
                "search_analyzer":"ik_smart"
            },
            "author": {
                "type": "keyword"
            },
            "category": {
                "type": "keyword"
            }
        }
    }
    '
    

    查看原索引 mapping

    curl -X GET "http://192.168.1.101:9200/test_source/_mapping?pretty"
    {
      "test_source" : {
        "mappings" : {
          "properties" : {
            "author" : {
              "type" : "keyword"
            },
            "category" : {
              "type" : "keyword"
            },
            "content" : {
              "type" : "text"
            },
            "title" : {
              "type" : "text"
            }
          }
        }
      }
    }
    
  3. 从原索引复制数据到新索引

    注意: 不宜用于复制数据量过大的索引

    curl -X POST "http://192.168.1.101:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
    {
      "source": {
        "index": "test_source"
      },
      "dest": {
        "index": "test_new"
      }
    }
    '
    
  4. 修改别名
    curl -X POST "http://192.168.1.101:9200/_aliases?pretty" -H 'Content-Type: application/json' -d'
    {
      "actions": [
        {
            "remove" : {
                "index" : "test_source", 
                "alias" : "test" 
            } 
        },
        {
          "add": {
            "index": "test_new",
            "alias": "test"
          }
        }
      ]
    }
    '
    
  5. 删除旧索引 test_source
    curl -X DELETE "http://192.168.1.101:9200/test_source?pretty"
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值