Elasticsearch-py: How to manage Elasticsearch connection

Created on 25 Nov 2018  路  7Comments  路  Source: elastic/elasticsearch-py

How to manage Elasticsearch connection in a Flask application. Should I create a global connection for all users or a connection to each user, in global or local scope ?

question

All 7 comments

Hi @Sphinxs ! What kind of application are you developing? Is it a web app with something like Flask or a command line app or something else?

Hi, @Winterflower . I'm developming a web application using Flask. I have modeled a class to create the connection to Elasticsearch and two other classes that extended the connection. In the routes I create an instance of the connection, in global scope.

the best way to think of working with Elasticsearch is that you connect to Elastic over a REST api. So it, does not work the same way a connection to SQL would work using SQL alchemy.

When you are connecting to a rest api you don't open a connection and then leave it open.
So the same applies when using Elasticsearch.

When you need to connect to ES you instanciate the connection transmit your data and then let the python garbage collection handle the rest.

@fxdgear , the best case it's to create the connection inside each route, get the resources and let the garbage collector handle the instance ? With this approach I can implement the class methods as @classmethod and remove the necessity of class instances

I'm not super familiar with Flask but yeah lets say you have a route that's /search/ it's tied to a function named search.

def search(*args, *kwargs):
    es = Elasticsearch('host')
    results = es.search(**kwargs)
    return results

You could also define es in your global namespace, but I'd prefer it here.

You could also create a decorator that would instantiate the connection as well...

Awesome, thanks.

@Sphinxs I'm closing this ticket since it seems that your question has been answered.

Please feel free to reopen it if you need to follow up on this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alanbacon picture alanbacon  路  3Comments

tlinhart picture tlinhart  路  5Comments

jaisharma639 picture jaisharma639  路  6Comments

peta15 picture peta15  路  6Comments

gcornut picture gcornut  路  5Comments