Search code examples
pythonpython-3.xelasticsearchelasticsearch-dsl

ElasticSearch Python Index and Alias Creation


I'm using the Python ElasticSearch DSL library to interface with an ElasticSearch cluster.

I use the Document ViewModel that the library provides through the base class elasticsearch_dsl.DocType and was using DocType.init() to create my indices previously:

import elasticsearch_dsl as dsl

class SomeDocument(dsl.DocType):
    class Meta:
        doc_type = some_document
        index = some_document

instance = SomeDocument()
instance.init()

But now I want to add an alias to the indices created here on Elastic's initialization, as well as change the number of shards of the indices. The code that I've come up for that is:

import elasticsearch_dsl as dsl

class SomeDocument(dsl.DocType):
    class Meta:
        doc_type = some_document
        index = some_document

instance = SomeDocument()

doc_index = dsl.Index('some_document_v1')
doc_index.aliases(some_document={})

if not doc_index.exists():
    doc_index.create()

My hope is that the SomeDocument ViewModel uses the some_document index for its requests, although some_document is now an alias that points to the index some_document_v1.

When I try to run this code, I get:

index some_document_v1 already exists

Even though I am checking index.exists() before calling index.create(). If I clear the ElasticSearch instance of its data, the server just crashes and responds 400 Bad Request to any request I send to it.

What am I doing wrong here?


Solution

  • Found what I was looking for here:

    https://github.com/elastic/elasticsearch-dsl-py/issues/600

    It turns out the elastic dsl gets "too confusing" internally at this point for them to do the right configuration with aliases. They suggest that since the consumer has all the pieces, build the structure manually instead of having the DocType.init() build it for you.