本文介绍: 如果不允许修改索引字段类型,只能重建索引步骤。
如果不允许修改索引字段类型,只能重建索引
步骤
- 新建一个索引
- 数据迁移
- 删除旧索引
- 别名引用
目录
1、准备工作
1.1、查看版本号
GET /
响应
{
"name": "jxZZibZ",
"cluster_name": "elasticsearch",
"cluster_uuid": "23IcxgHqTniM4wOGyl03Pw",
"version": {
"number": "5.6.16",
"build_hash": "3a740d1",
"build_date": "2019-03-13T15:33:36.565Z",
"build_snapshot": false,
"lucene_version": "6.6.1"
},
"tagline": "You Know, for Search"
}
可以看到,我所使用的ES 版本号为:5.6.16
1.2、创建旧索引
旧索引名为old-index
,包含2个字段:id
、username
PUT /old-index
{
"mappings": {
"doc": {
"properties": {
"id": {
"type": "integer"
},
"username": {
"type": "text"
}
}
}
}
}
响应
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "old-index"
}
1.3、添加两条数据
POST /old-index/doc
{
"id": 1000,
"username": "root"
}
POST /old-index/doc
{
"id": 1001,
"username": "other"
}
1.4、查看数据
GET /old-index/_search
响应
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "old-index",
"_type": "doc",
"_id": "AY1pDh-BNp3AgKn6mtEj",
"_score": 1,
"_source": {
"id": 1000,
"username": "root"
}
},
{
"_index": "old-index",
"_type": "doc",
"_id": "AY1pDikjNp3AgKn6mtEk",
"_score": 1,
"_source": {
"id": 1001,
"username": "other"
}
}
]
}
}
2、新建一个索引
2.1、查看旧索引的mapping
GET /old-index/_mapping
响应
{
"old-index": {
"mappings": {
"doc": {
"properties": {
"id": {
"type": "integer"
},
"username": {
"type": "text"
}
}
}
}
}
}
2.2、新建索引
新的索引名为new-reindex
,将id
字段的类型从integer
改为keyword
PUT /new-reindex
{
"mappings": {
"doc": {
"properties": {
"id": {
"type": "keyword"
},
"username": {
"type": "text"
}
}
}
}
}
3、数据迁移
3.1、使用异步任务迁移数据
POST /_reindex?wait_for_completion=false
{
"source": {
"index": "old-index",
"size": 10000
},
"dest": {
"index": "new-index"
}
}
响应
{
"task": "jxZZibZPQ_mViMJHQyFz5w:7335"
}
如果数据量较大,容易出现 Gateway Time-out
,所以我添加了参数wait_for_completion=false
,让其后台执行
{
"statusCode": 504,
"error": "Gateway Time-out",
"message": "Client request timeout"
}
3.2、查看任务状态
GET /_tasks/jxZZibZPQ_mViMJHQyFz5w:7335
{
"completed": true,
"task": {
"node": "jxZZibZPQ_mViMJHQyFz5w",
"id": 7335,
"type": "transport",
"action": "indices:data/write/reindex",
"status": {
"total": 2,
"updated": 0,
"created": 2,
"deleted": 0,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0
},
"description": "reindex from [old-index] to [new-index]",
"start_time_in_millis": 1706864890889,
"running_time_in_nanos": 105811530,
"cancellable": true
},
"response": {
"took": 105,
"timed_out": false,
"total": 2,
"updated": 0,
"created": 2,
"deleted": 0,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0,
"failures": []
}
}
4、删除旧索引
4.1、确认索引数据
查看新旧索引中的数据总量是否相等
GET /old-index/_count
GET /new-index/_count
响应
{
"count": 2,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
}
}
4.2、删除旧索引
确认数据已迁移完成,可以删除旧的索引了
DELETE /old-index
响应
{
"acknowledged": true
}
5、别名引用
5.1、创建别名
PUT /new-index/_alias/old-index
响应
{
"acknowledged": true
}
5.2、查看别名
GET _cat/aliases
old-index new-index - - -
5.3、使用别名
新旧索引都可以使用了
GET /old-index/_search
GET /new-index/_search
参考文章
原文地址:https://blog.csdn.net/mouday/article/details/135998284
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_65643.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。