Skip to content

Instantly share code, notes, and snippets.

@yanglikun
Created June 27, 2017 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanglikun/0f023acbfd4b5cc430e0c90996b46389 to your computer and use it in GitHub Desktop.
Save yanglikun/0f023acbfd4b5cc430e0c90996b46389 to your computer and use it in GitHub Desktop.
es-reindex
__author__ = 'yanglikun'
import elasticsearch
import elasticsearch.helpers
esSource=elasticsearch.Elasticsearch([{"host":"192.168.147.90","port":20000}])
esDestination=elasticsearch.Elasticsearch([{"host":"192.168.147.90","port":20000}])
sourceIndex="mock_project_index"
sourceType="mock_project_type"
mapping='''
{
"mappings": {
"{esType}": {
"dynamic": "strict",
"_all": {
"enabled": false
},
"properties": {
"owner": {
"type": "string",
"index":"not_analyzed"
},
"created": {
"format": "yyyy-MM-dd HH:mm:ss",
"type": "date"
},
"projectName": {
"index": "not_analyzed",
"type": "string"
},
"desc": {
"index": "no",
"type": "string"
}
}
}
}
}
'''.replace('{esType}',sourceType)
tmpIndex=sourceIndex+"ylk_tmp"
esDestination.indices.delete(tmpIndex,ignore=[400,404])
esDestination.indices.create(index=tmpIndex,body=mapping)
elasticsearch.helpers.reindex(client=esSource,source_index=sourceIndex,target_index=tmpIndex,target_client=esDestination)
esSource.indices.delete(sourceIndex)
esSource.indices.create(index=sourceIndex,body=mapping)
elasticsearch.helpers.reindex(client=esDestination,source_index=tmpIndex,target_index=sourceIndex,target_client=esSource)
esDestination.indices.delete(tmpIndex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment