Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

DRF wrapper around ElasticsearchDSL

Project description

DRF Elasticsearch DSL is losely based on django-haystack and provides a ModelSerializerDocument which supports all of the field types provided by elastic-search-dsl persistence.. ModelSerializerDocument is losely based on the DocType class provided by

The purpose of this libraray is to allow definition of elasticsearch documents with DRF’s ModelSerializer class while optionally providing support for async document updates and deletes with celery.


The full documentation is at


Install Django Package:

pip install drf-elasticsearch-dsl

Add it to your INSTALLED_APPS:


Configure DRF_SERIALIZER_ELASTICSERACH_SETTTINGS in your file with your elasticsearch url(s)

    'elasticsearch_hosts': ['localhost']

Create a Model

from django.db import models

class Contact(models.Model):

    first_name = models.CharField(max_length=32, null=False, blank=False)
    last_name = models.CharField(max_length=32, null=False, blank=False)
    url = models.URLField(null=False, blank=False)
    email = models.EmailField(max_length=254, null=False, blank=False)
    bio = models.TextField(null=False, blank=False)
    birthday = models.DateField(null=False, blank=False)

Create a ModelSerializer

from rest_framework import serializers

class ContactSerializer(serializers.ModelSerializer):

    class Meta:
        model = Contact
        fields = '__all__'

Create a, which should be in the root of the application. Add your ModelSerializerDocument classes here. The specificed index will have its mapping updated for this document.

from drf_elasticsearch_dsl.documents import ModelSerializerDocument
from elasticsearch_dsl import Date, Keyword, Text, String
from .serialziers import ContactSerializer

class ContactSerializerDocument(ModelSerializerDocument):
    first_name = String()
    last_name = String()
    url = Keyword()
    email = Keyword()
    bio = Text()
    birthday = Date()

    class Meta:
        index = 'myapp'
        serializer = ContactSerializer
        doc_type = ''

Finally, sync your database with elasticsearch by running:

$ python update_index


Celery Support

By default, dr-elasticsearch-dsl does not setup signals to sync models on save or delete. To enable celery support, add the following to your confiration:

    'signal_processor_class': 'drf_elasticsearch_dsl.signals.CelerySignalProcessor',

See the celery documentation for details setting up celery with django

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements_test.txt
(myenv) $ tox


  • Add search URLS to be automatically added to all ModelSerializerDocument added to
  • Better documentation
  • Better test coverage


Tools used in rendering this package:

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for drf-elasticsearch-dsl, version 0.1.4
Filename, size File type Python version Upload date Hashes
Filename, size drf_elasticsearch_dsl-0.1.4-py2.py3-none-any.whl (14.1 kB) File type Wheel Python version 3.6 Upload date Hashes View
Filename, size drf-elasticsearch-dsl-0.1.4.tar.gz (10.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page