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"
    
  1. 创建新索引 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"
            }
          }
        }
      }
    }
    
  2. 从原索引复制数据到新索引

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

    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"
      }
    }
    '
    
  3. 修改别名
    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"
          }
        }
      ]
    }
    '
    
  4. 删除旧索引 test_source
    curl -X DELETE "http://192.168.1.101:9200/test_source?pretty"
    
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容