ElasticSearch的reIndex

来源:互联网 发布:谷歌输入法mac版 编辑:程序博客网 时间:2024/05/21 21:26

由于Elasticsearch已创建的index是不允许修改的,其原因是由于创建的时候就会创建倒排索引,但是此时应用程序已经在使用了该索引,并且不允许应用暂停,那么怎么去处理这样的事情呢?下面就是一个比较靠谱的解决方案,就是使用alias,scroll api,bucket api。

具体案例如下:

保存数据
PUT /index_before/type_before/1
{
"title":"2017-01-01"
}
查询数据
GET index_before/_mapping/type_before
结果发现类型不对
{
"index_before": {
"mappings": {
"type_before": {
"properties": {
"title": {
"type": "date"
}
}
}
}
}
}

设置别名,index_mid中间过渡(java程序可以使用别名的index),指向之前的索引
PUT index_before/_alias/index_mid

新建正确类型的索引
PUT /index_after
{
"mappings": {
"type_before":{
"properties": {
"title":{
"type": "string"
}
}
}
}
}


使用scroll api获得原始数据
GET index_before/_search?scroll=1m
{
"query": {
"match_all": {}
},
"sort": [
"_doc"
],
"size": 1
}


使用bulk api将查询的数据转存到新的索引中去
POST /_bulk
{"index":{"_index":"index_after","_type":"type_before","_id":1}}
{"title":"2017-01-01"}

更改索引别名

POST /_aliases
{
"actions": [
{ "remove": { "index": "index_before", "alias": "index_mid" }},
{ "add":{ "index": "index_after", "alias": "index_mid" }}
]
}

使用别名查询
GET index_mid/type_before/_search
结果就可以了
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "index_after",
"_type": "type_before",
"_id": "1",
"_score": 1,
"_source": {
"title": "2017-01-01"
}
}
]
}
}


原创粉丝点击