pythonelasticsearchflask

How to connect to Elasticsearch using Python Flask


I'm writing a site on Flask, I decided to make a search system on the site using Elasticsearch, I did it according to the guide https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvi-full-text-search .

Here is my code:

es = Elasticsearch(os.environ.get('ELASTICSEARCH_URL'))
app.elasticsearch = Elasticsearch([app.config['ELASTICSEARCH_URL']]) if app.config['ELASTICSEARCH_URL'] else None

But I get an error:

app.elasticsearch = Elasticsearch([app.config['ELASTICSEARCH_URL']]) if app.config['ELASTICSEARCH_URL'] else None
KeyError: 'ELASTICSEARCH_URL'

Please help to fix this.


Solution

  • I think you have simply not degined the elasticsearch key inside your app config. According to the documentation you should first make

    app.config['ELASTICSEARCH_URL'] = os.environ.get('ELASTICSEARCH_URL')
    
    

    In the end the app config behaves as a dictionary, and you are trying to access a key that does not exist