需求:index 的mapping有改动,需要加上smartcn analyzer,数据需要从旧的index导出并导入新的index
注意:代码示例中的index/type name是瞎写的,请自行修改 ...
第一步:以新的mapping创建index,加上alias.
如果旧的index没有加alias,此时也要加上。其实旧的index应该一开始创建的时候就加上alias,并且代码中是去连alias,而不是具体的index name,这样为以后修改mapping提供便利
创建mapping:
curl -XPUT http://127.0.0.1:9200/chat -d '
{
"settings" : {
"number_of_shards" : 5
},
"mappings" : {
"CHAT" : {
"properties" : {
"eid" : {
"type" : "long"
},
"kvs" : {
"properties" : {
"DESCRIPTION" : {
"type" : "string",
"analyzer" : "smartcn"
},
"NAME" : {
"type" : "string",
"analyzer" : "smartcn"
}
}
},
"tn" : {
"type" : "string"
}
}
},
"TOPIC" : {
"properties" : {
"eid" : {
"type" : "long"
},
"kvs" : {
"properties" : {
"CONTENT" : {
"type" : "string",
"analyzer" : "smartcn"
},
"SUBJECT" : {
"type" : "string",
"analyzer" : "smartcn"
}
}
},
"tn" : {
"type" : "string"
}
}
}
}
}
创建alias:
curl -XPOST localhost:9200/_aliases -d '
{
"actions": [
{ "add": {
"alias": "my_index",
"index": "my_index_v1"
}}
]
}
'
参考:点击打开链接http://blog.csdn.net/lein_wang/article/details/51754922
第二步:导出/导入数据
参考 http://www.oschina.net/code/snippet_3712_49869, 在人家的基础上修改了一下,原先不支持bulk,效率太低
需要修改其中的index / type 为你想要的
>>> python esutil.py
#!/usr/bin/python
#coding:utf-8
'''
Export and Import ElasticSearch Data.
Simple Example At __main__
@author: wgzh159@163.com
@note: uncheck consistency of data, please do it by self
'''
import json
import os
import sys
import time
import urllib2
reload(sys)
sys.setdefaultencoding('utf-8') # @UndefinedVariable
class exportEsData():
size = 10000
def __init__(self, url,index,type,target_index):
self.url = url+"/"+index+"/"+type+"/_search"
self.index = index
self.type = type
self.target_index = target_index #替换原有的index
self.file_name = self.target_index+"_"+self.type+".json"
def exportData(self):
print("export data begin...\n")
begin = time.time()
try:
os.remove(self.file_name)
except:
os.mknod(self.file_name)
msg = urllib2.urlopen(self.url).read()
print(msg)
obj = json.loads(msg)
num = obj["hits"]["total"]
start = 0
end = num/self.size+1
while(start