elasticsearch修改mapping + 导出/导入数据

需求: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
   
   
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值