Skip to content

Instantly share code, notes, and snippets.

@cinhtau
Created February 5, 2018 16:03
Show Gist options
  • Save cinhtau/2ea0398e9951e5ff9d24493b9def7849 to your computer and use it in GitHub Desktop.
Save cinhtau/2ea0398e9951e5ff9d24493b9def7849 to your computer and use it in GitHub Desktop.
Simple Reindex script using the Elasticsearch Reindex API, loop over a month, for each day print respective index and create target index for reindex. The target index has only 1 shard.
import requests
from datetime import timedelta, date
def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
yield start_date + timedelta(n)
headers={'Content-Type', 'application/json'}
base_url='http://elasticsearch:9200'
start_date=date(2017, 5, 7)
end_date=date(2017, 5, 31)
for single_date in daterange(start_date, end_date):
current_day = single_date.strftime("%Y.%m.%d")
r = requests.get(base_url + '/_cat/indices/fo-log-' + current_day, auth=('le-mapper', 'MapperKing'))
print r.text
r = requests.put(base_url + '/fo-log-fix-' + current_day, auth=('le-mapper', 'MapperKing'), json={"settings":{"index.routing.allocation.require.box_type":"superhot","number_of_shards":1,"number_of_replicas":0,"refresh_interval":-1}})
print r.text
r = requests.post(base_url + '/_reindex', auth=('le-mapper', 'MapperKing'), json={"source":{"index":"fo-log-" + current_day },"dest":{"index":"fo-log-fix-" + current_day,"pipeline":"improve"}})
print r.text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment