Skip to main content
https://badge.fury.io/py/drf-elasticsearch-dsl.svg https://circleci.com/gh/ajbeach2/drf-elasticsearch-dsl.svg?style=svg https://codecov.io/gh/ajbeach2/drf-elasticsearch-dsl/branch/master/graph/badge.svg

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 elasticsearch-dsl.py

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.

Documentation

The full documentation is at https://drf-elasticsearch-dsl.readthedocs.io.

Quickstart

Install Django Package:

pip install drf-elasticsearch-dsl

Add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'drf_elasticsearch_dsl.apps.DrfElasticsearchDsl',
    ...
)

Configure DRF_SERIALIZER_ELASTICSERACH_SETTTINGS in your settings.py file with your elasticsearch url(s)

DRF_SERIALIZER_ELASTICSERACH_SETTTINGS = {
    '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 search_indexes.py, 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 = 'myapp.contact'

Finally, sync your database with elasticsearch by running:

$ python manage.py update_index

Features

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 settings.py confiration:

DRF_SERIALIZER_ELASTICSERACH_SETTTINGS = {
    ...
    '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

TODO:

  • Add search URLS to be automatically added to all ModelSerializerDocument added to search_indexes.py

  • Better documentation

  • Better test coverage

Credits

Tools used in rendering this package:

Release files for drf-elasticsearch-dsl 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-elasticsearch-dsl 0.1.4
File Size Uploaded
drf-elasticsearch-dsl-0.1.4.tar.gz 10.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-elasticsearch-dsl 0.1.4
File Interpreter ABI Platform
drf_elasticsearch_dsl-0.1.4-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 24.8 kB

Release files / drf-elasticsearch-dsl-0.1.4.tar.gz

Download URL drf-elasticsearch-dsl-0.1.4.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2a0bcbf0b2572e78d37dd138defe7a957a890da30291ed0e36b94d383f946abf
BLAKE2b-256 checksum
How to use checksums
e14091582c02fd433beac1f554e5b3a3e1914c38e295d61b607be6721783556d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / drf_elasticsearch_dsl-0.1.4-py2.py3-none-any.whl

Download URL drf_elasticsearch_dsl-0.1.4-py2.py3-none-any.whl
Size 14.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8aa56decb09e160e4d3009097d18c8c2e0c3b7eeb1a6a432425d483c1f1af334
BLAKE2b-256 checksum
How to use checksums
bb7934a6391198bce63454870277ca01816ba9a75e3154bbe6c1345a5bdaed59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page