Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save enixdark/fd8d896c0976e7339c25a18d98a83c44 to your computer and use it in GitHub Desktop.
Save enixdark/fd8d896c0976e7339c25a18d98a83c44 to your computer and use it in GitHub Desktop.

Revisions

  1. @ikeikeikeike ikeikeikeike created this gist Mar 30, 2015.
    54 changes: 54 additions & 0 deletions zero_downtime_reindexing.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    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}}
    ]
    })