Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Elasticsearch Zero Downtime Reindexing using elasticsearch-dsl-py ref: https://www.elastic.co/blog/changing-mapping-with-zero-downtime
from datetime import datetime
from elasticsearch_dsl import (
DocType,
String,
Integer,
Float
)
def _suffix():
return datetime.now().strftime("%Y%m%d%H%M%S%f")
class Company(DocType):
name = String(analyzer='kuromoji')
alias = String(analyzer='2gram')
languages = String(multi=True)
pref = Integer()
point = Float()
class Meta:
index = 'company'
def reindex():
"""
Elasticsearch Zero Downtime Reindexing using elasticsearch-dsl-py
"""
c = Company().connection
alias = Company._doc_type.index
# If alias does not exists, As first create new index.
if not c.indices.exists_alias(alias):
index = '%s_%s' % (alias, _suffix())
Company.init(index)
c.indices.put_alias(alias, index)
old_index = list(c.indices.get_alias(alias).keys())[0]
new_index = '%s_%s' % (alias, _suffix())
# Create new index to ES
Company.init(new_index)
# Put document to new index from Django model's records
dosomething(new_index)
# Change index through alias.
c.indices.put_alias(alias, new_index)
c.indices.update_aliases({
"actions": [
{"remove": {"index": old_index, "alias": alias}},
{"add": {"index": new_index, "alias": alias}}
]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment