如果不允许修改索引字段类型,只能重建索引
步骤
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
旧索引名为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"
}
POST /old-index/doc
{
"id": 1000,
"username": "root"
}
POST /old-index/doc
{
"id": 1001,
"username": "other"
}
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"
}
}
]
}
}
GET /old-index/_mapping
响应
{
"old-index": {
"mappings": {
"doc": {
"properties": {
"id": {
"type": "integer"
},
"username": {
"type": "text"
}
}
}
}
}
}
新的索引名为new-reindex
,将id
字段的类型从integer
改为keyword
PUT /new-reindex
{
"mappings": {
"doc": {
"properties": {
"id": {
"type": "keyword"
},
"username": {
"type": "text"
}
}
}
}
}
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"
}
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": []
}
}
查看新旧索引中的数据总量是否相等
GET /old-index/_count
GET /new-index/_count
响应
{
"count": 2,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
}
}
确认数据已迁移完成,可以删除旧的索引了
DELETE /old-index
响应
{
"acknowledged": true
}
PUT /new-index/_alias/old-index
响应
{
"acknowledged": true
}
GET _cat/aliases
old-index new-index - - -
新旧索引都可以使用了
GET /old-index/_search
GET /new-index/_search