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"
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章