重建索引且保证应用程序不重启

PUT /index1/type1/4

{
  "content":"1990-12-12"
}

添加成功:

{
  "_index": "index1",
  "_type": "type1",
  "_id": "4",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

GET /index1/type1/_mapping
查看mapping

{
  "index1": {
    "mappings": {
      "type1": {
        "properties": {
          "content": {
            "type": "date"
          }
        }
      }
    }
  }
}

PUT /index1/type1/5

{
  "content":"I like JAVA"
}

添加失败:

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "failed to parse [content]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "failed to parse [content]",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Invalid format: \"I like JAVA\""
    }
  },
  "status": 400
}

要想实现添加字符串类型的,那么必须把content的类型修改为string
修改字段content的类型
PUT /index1/_mapping/type1

{
  "properties": {
    "content":{
      "type": "text"
    }
  }
}

报错

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "mapper [content] of different type, current_type [date], merged_type [text]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "mapper [content] of different type, current_type [date], merged_type [text]"
  },
  "status": 400
}

字段的类型一旦确定就不能修改了,要想修改字段的类型,只能新建一个新的索引,新的索引的字段类型是string类型,把旧的索引中的数据再导入到新的索引中,但是,如果新建一个索引,那么在应用程序中使用的是原有的索引,那么就会导致需要重新启动应用程序,为了不用重启应用,我们使用别名的方式

PUT /index1/_alias/index2

创建新的索引
PUT /newindex

{
  "mappings": {
    "type1":{
      "properties":{
        "content":{
          "type":"text"
        }
      }
    }
  }
}

把旧的索引中的数据再导入到新的索引中,有可能旧的索引中的数据量非常大,使用scroll方式批量查询数据,然后使用bulk再批量添加到新的索引中
GET index1/type1/_search?scroll=1m

{
  "query": {
    "match_all": {}
  },
  "sort": ["_doc"],
  "size": 2
}

POST /_bulk

{"index":{"_index":"newindex","_type":"type1","_id":1}}
{"content":"1990-12-12"}

把新的索引和别名进行关联
POST /_aliases

{
  "actions": [
    {
      "remove": {
        "index": "index1",
        "alias": "index2"
      }
    },
    {
      "add": {
        "index": "newindex",
        "alias": "index2"
      }
    }
  ]
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容