Forked from ikeikeikeike/zero_downtime_reindexing.py
Created
September 5, 2022 18:01
-
-
Save enixdark/fd8d896c0976e7339c25a18d98a83c44 to your computer and use it in GitHub Desktop.
Elasticsearch Zero Downtime Reindexing using elasticsearch-dsl-py ref: https://www.elastic.co/blog/changing-mapping-with-zero-downtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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