Skip to content

Instantly share code, notes, and snippets.

@ikeikeikeike
Created March 30, 2015 04:15
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ikeikeikeike/f17efe77aafd5d0b61b0 to your computer and use it in GitHub Desktop.
Save ikeikeikeike/f17efe77aafd5d0b61b0 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
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