WisdomPill
WisdomPill

Reputation: 800

Integrating elasticsearch connection pooling in Django

I have a django application that stores status info of lots of sensors and I'm trying to use elasticsearch to store all of the status data, for further analysis.

I have used elasticsearch a lot in Python scripts in general.

My question is how to integrate elasticsearch-py/elasticsearch-dsl connection pooling into a django application. I would like to not create a connection for every request. I'm actually trying to embeed it into MyAppConfig inside apps.py but I'm not sure if it's a good idea.

Any help?

Upvotes: 2

Views: 1082

Answers (1)

WisdomPill
WisdomPill

Reputation: 800

Elasticsearch-dsl can be integrated easily using the ready app function in the following way.

/init.py

default_app_config = '<app>.apps.AppConfig'

/apps.py

import logging

from django.apps import AppConfig
from django.conf import settings
from elasticsearch_dsl.connections import connections

log = logging.getLogger('elastic')


class AppConfig(AppConfig):
    name = <app_name>

    def ready(self):
        connections.configure(**settings.ELASTICSEARCH)

then in django settings I have a dictionary called elasticsearch with alias configurations

ELASTICSEARCH = {
    'default': {
        'hosts': [
            {
                'host': <your_host>,
                'port': <your_host_port>,
                'verify_certs': True,
                'use_ssl': True,
                'http_auth': (
                    <auth_name>,
                    <auth_passwd>
                )
            }
        ]
    },
    'dev': {
        'hosts': [
            {
                'host': 'localhost',
                'port': 9200
            }
        ]
    }
}

Upvotes: 4

Related Questions