如果不允许修改索引字段类型,只能重建索引
步骤
- 新建一个索引
- 数据迁移
- 删除旧索引
- 别名引用
目录
-
- 1、准备工作
-
- 1.1、查看版本号
- 1.2、创建旧索引
- 1.3、添加两条数据
- 1.4、查看数据
- 2、新建一个索引
-
- 2.1、查看旧索引的mapping
- 2.2、新建索引
- 3、数据迁移
-
- 3.1、使用异步任务迁移数据
- 3.2、查看任务状态
- 4、删除旧索引
-
- 4.1、确认索引数据
- 4.2、删除旧索引
- 5、别名引用
-
- 5.1、创建别名
- 5.2、查看别名
- 5.3、使用别名
- 参考文章
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": 1706864890服务器托管网889,
"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
参考文章
- 如何在Elasticsearch中改变字段类型?
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: MySQL 8.0.26版本升级32版本查询数据为空的跟踪
3月16日,北京源创会 —— “数据库,2024 开炫” 某业务系统将MySQL 8.0.26升级为GreatSQL 8.0.32-24 后,某些特定的SQL语句不能查询到数据。经测试 MySQL 8.0.32也存在相同的问题 此BUG已在 GreatSQL …